代码改变世界

WCF初接触实作——服务的发布和使用

2011-12-28 10:42  Higel  阅读(2606)  评论(4编辑  收藏  举报

之前两篇随笔的示例中客户端直接引用契约类库,现实中可能因为开发团队或语言等原因,客户端不能直接引用契约类库,这就需要服务端公布自己的契约、客户端发现契约。

服务端:

服务端通过配置服务行为,以元数据的形式公布服务。可以使用配置文件也可以使用代码。

1、使用配置文件:

将之前的WCFDemo.Host.WithConfig项目的配置文件用WCF服务配置编辑器打开,新建服务行为配置:

这里就用默认的Name,实际项目中起个好听的名字吧☺

添加服务元数据:

设置元数据的HttpGetEnabledHttpGetUrl

选择服务,设置其BehaviorConfiguration为刚添加的服务行为:

保存后的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior0">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:5678/DemoService/metadata" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior0" name="WCFDemo.Services.DemoService">
<endpoint address="http://localhost:5678/DemoService" binding="basicHttpBinding"
bindingConfiguration="" contract="WCFDemo.Contracts.IDemoService" />
</service>
</services>
</system.serviceModel>
</configuration>

使用配置文件的方式,程序代码不需要任何修改。

2、代码方式:

WCFDemo.Host.WithoutConfig项目的启动服务代码处添加服务行为的处理代码:

            host = new ServiceHost(typeof(DemoService));
host.AddServiceEndpoint(typeof(IDemoService), new BasicHttpBinding(), "http://localhost:5678/DemoService");
ServiceMetadataBehavior b = new ServiceMetadataBehavior();
b.HttpGetEnabled = true;
b.HttpGetUrl = new Uri("http://localhost:5678/DemoService/metadata");
host.Description.Behaviors.Add(b);
host.Opened += delegate { label1.Text = "服务启动"; };
host.Open();

比原来多了4行添加服务行为的代码。

现在,运行两个宿主程序中的任意一个,点击启动按钮后,服务就启动并发布了,客户端可以发现契约并使用。

 

客户端:

客户端如何发现并使用服务,有2种方式:使用命令行svcutil生成文件、在IDE中添加服务引用。

1、使用svcutil

运行宿主并启动服务;运行Visual Studio 命令提示,键入svcutil http://localhost:5678/DemoService/metadata,将生成一个DemoService.cs文件和一个output.config文件(可以通过/out:指定输出目录);

在解决方案中添加一个Windows窗体应用程序WCFDemo.Client,为其添加引用System.ServiceModelSystem.Runtime.Serialization

将刚才生成的两个文件添加到项目,并将output.config改名为App.config

在窗体上放置一个ButtonDataGridView,为ButtonClick编写代码如下:

DemoServiceClient c = new DemoServiceClient();
dataGridView1.DataSource = c.GetMonitorData();

当然可以不用配置文件,new DemoServiceClient()中设置参数binding和remoteAddress。

svcutil常用的选项有/out:、/config:、/noconfig:等,详细用法这里就不介绍了。查看配置文件会发现里面内容很多,因为它自动为关键的绑定节点设置了默认值,这部分内容可以删除,所以很多时候不使用svcutil生成的配置文件。

2、添加服务引用

右击WCFDemo.Client,在添加服务引用对话框中输入地址http://localhost:5678/DemoService/metadata,点击“前往”按钮:

给命名空间起个好名(示例中就用默认名)后确定。

我们会发现,除了添加了服务引用,还修改了配置文件,如果原来没有配置文件,添加服务引用后会自动添加配置文件。

在窗体上再放置一个Button,为其Click编写代码如下:

ServiceReference1.DemoServiceClient c = new ServiceReference1.DemoServiceClient();
dataGridView1.DataSource = c.GetMonitorData();

和前一个一样,可以不用配置文件。

 

服务器有两种方案发布自己的元数据:基于HTTP-GET协议、使用专门的终结点。以上介绍的是前一种,下面介绍一下第二种。

1、使用配置文件

将之前的WCFDemo.Host.WithConfig项目的配置文件用WCF服务配置编辑器打开,新建服务终结点,并设置ABC:

现在WCFDemo.Host.WithConfig已提供两种发布服务的方式,启动服务后,客户端通过之前的地址http://localhost:5678/DemoService/metadata和刚才输入的地址http://localhost:5678/DemoService/MEX,都可以找到服务。

2、使用代码方式

在WCFDemo.Host.WithoutConfig项目的启动服务代码处增加一行添加终结点代码:

            host = new ServiceHost(typeof(DemoService));
host.AddServiceEndpoint(typeof(IDemoService), new BasicHttpBinding(), "http://localhost:5678/DemoService");

ServiceMetadataBehavior b = new ServiceMetadataBehavior();
b.HttpGetEnabled = true;
b.HttpGetUrl = new Uri("http://localhost:5678/DemoService/metadata");
host.Description.Behaviors.Add(b);

host.AddServiceEndpoint(typeof(IMetadataExchange), new CustomBinding(new HttpTransportBindingElement()), "http://localhost:5678/DemoService/MEX");

host.Opened += delegate { label1.Text = "服务启动"; };
host.Open();

效果同上。