WCF之HelloWorld

不说闲话了,直接hello了。

创建服务

1、  选择file-->new-->project-->empty project创建一空项目,把solution命名为wcfHello。

2、  右击wcfHello项目,add-->new item添加接口IHello。在属性页把output type改为class library。

3、  在接口中添加一方法,

public interface IHelloService
{
string HelloWorld();
}

4、  Add References添加对程序集System.ServiceModel的引用。

using System.ServiceModel;

5、  用ServiceContractAttribute修饰IHello接口使其成为服务契约,同时用OperationContractAttribute修饰方法HelloWorld(),使其包含在服务契约中。

 

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

6、  添加服务类HelloService.cs。

 

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

寄存服务

1、  添加Console Application项目,命名为HelloHost。

2、  引用System.ServiceModel程序集。

3、  添加引用项目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();
}
}
}
}

创建代理调用服务

1、  添加一Console Application项目,命名为HelloClient。

2、  引用System.ServiceModel程序集。

3、  复制契约到客户端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();
}
}
}

运行结果如下:

查看HelloService元数据,修改HelloHost项目如下:

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();
}
}

运行服务器端服务,在浏览器重查看元数据如图

ServiceModel Metadata Utility

ServiceModel Metadata Utility是一个命令工具,叫做svcutil.exe的可执行文件,用它可以生成客户端代理类和配置文件,如图

执行后在“D:\visual studio 2010\Projects\wcfHello\HelloClient”下面生成一个app.config配置文件和ServiceProxy.cs类。

app.config的内容如:

而后修改客户端代码:

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();
}

运行结果:

posted @ 2011-04-06 08:17  秋无语  阅读(645)  评论(0编辑  收藏  举报