Helloworld of WCF

Source: Internet
Author: User

If you don't talk about it, you can just say hello.

Create a service

1. Select File --> New --> project --> empty project to create an empty project and name solution wcfhello.

2. Right-click the wcfhello project and add --> new item to add the ihello interface. On the property page, change output type to class library.

3. Add a method to the interface,

public interface IHelloService
{
string HelloWorld();
}

4. Add references to add a reference to the assembly system. servicemodel.

using System.ServiceModel;

5. Use servicecontractattribute to modify the ihello interface as a service contract, and use the operationcontractattribute modifier helloworld () to include it in the service contract.

 

[ServiceContract(Namespace="http://www.cnblogs.com/qiuwuyu")]
public interface IHelloService
{
[OperationContract]
string HelloWorld();
}

6. Add the service class helloservice. CS.

 

 public class HelloService: IHelloService
{
public string HelloWorld()
{
return "HelloWorld_wcf";
}
}

Storage Service

1. Add Console
Application project, named hellohost.

2. reference the system. servicemodel assembly.

3. Add the reference project wcfhello.

 

using System;
using System.ServiceModel;
using wcfHello;

namespace HelloHost
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HelloService),
new Uri("http://localhost:8000/HelloWcf")))
{
host.AddServiceEndpoint(typeof(IHelloService), new BasicHttpBinding(), "HelloWcf");
host.Open();
Console.WriteLine("Hello Service Is Running...");
Console.ReadLine();
}
}
}
}

Create proxy call service

1. Add a console
Application project, named helloclient.

2. reference the system. servicemodel assembly.

3. Copy the contract to the client ihelloservice.

 

using System.ServiceModel;
namespace HelloClient
{
[ServiceContract(Namespace = "http://www.cnblogs.com/qiuwuyu")]
public interface IHelloService
{
[OperationContract]
string HelloWorld();
}
}

using System;
using System.ServiceModel;

namespace HelloClient
{
class Program
{
static void Main(string[] args)
{
EndpointAddress epAddr = new EndpointAddress("http://localhost:8000/HelloWcf/HelloService");
IHelloService proxy = ChannelFactory<IHelloService>.CreateChannel(new BasicHttpBinding(), epAddr);
Console.WriteLine(proxy.HelloWorld());
Console.WriteLine("Client Is Running...");
Console.ReadLine();
}
}
}

The running result is as follows:

View the helloservice metadata and modify the hellohost project as follows:

static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HelloService),
new Uri("http://localhost:8000/HelloWcf")))
{
host.AddServiceEndpoint(typeof(IHelloService), new BasicHttpBinding(), "HelloService");

ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(behavior);
host.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

host.Open();
Console.WriteLine("Hello Service Is Running...");
Console.ReadLine();
}
}

Run the server service and re-view the metadata in the browser

Servicemodel metadata Utility

Servicemodel metadata utilityis an executable file called svcutil.exe. It can be used to generate client proxy classes and configuration files,

After execution, an app. config configuration file and serviceproxy. CS class are generated under "D: \ Visual Studio 2010 \ projects \ wcfhello \ helloclient.

The content of APP. config is as follows:

Then modify the client code:

static void Main(string[] args)
{
//EndpointAddress epAddr = new EndpointAddress("http://localhost:8000/HelloWcf/HelloService");
//IHelloService proxy = ChannelFactory<IHelloService>.CreateChannel(new BasicHttpBinding(), epAddr);
HelloServiceClient proxy = new HelloServiceClient();
Console.WriteLine(proxy.HelloWorld());
Console.WriteLine("Client Is Running...");
Console.ReadLine();
}

Running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.