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

开发 后端
我们直接引用契约类库,现实中可能因为开发团队或语言等原因,客户端不能直接引用契约类库,这就需要服务端公布自己的契约、客户端发现契约。

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

  服务端:

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

  1、使用配置文件:

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

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

  添加服务元数据:

设置元数据的HttpGetEnabled和HttpGetUrl:

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

保存后的配置文件:

 

  1.   <?xml version="1.0"encoding="utf-8"?> 
  2.   <configuration> 
  3.   <system.serviceModel> 
  4.   <behaviors> 
  5.   <serviceBehaviors> 
  6.   <behavior name="NewBehavior0"> 
  7.   <serviceMetadata httpGetEnabled="true"httpGetUrl="http://localhost:5678/DemoService/metadata"/> 
  8.   </behavior> 
  9.   </serviceBehaviors> 
  10.   </behaviors> 
  11.   <services> 
  12.   <service behaviorConfiguration="NewBehavior0"name="WCFDemo.Services.DemoService"> 
  13.   <endpoint address="http://localhost:5678/DemoService"binding="basicHttpBinding"  
  14. bindingConfiguration=""contract="WCFDemo.Contracts.IDemoService"/>  
  15. bindingConfiguration=""contract="WCFDemo.Contracts.IDemoService"/>  
  16. bindingConfiguration=""contract="WCFDemo.Contracts.IDemoService"/> 
  17.   </service> 
  18.   </services> 
  19.   </system.serviceModel> 
  20.   </configuration> 

 

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

  2、代码方式:

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

 

  1.   host = newServiceHost(typeof(DemoService));  
  2.   host.AddServiceEndpoint(typeof(IDemoService), newBasicHttpBinding(), "http://localhost:5678/DemoService");  
  3.   ServiceMetadataBehavior b = newServiceMetadataBehavior();  
  4.   b.HttpGetEnabled = true;  
  5.   b.HttpGetUrl = newUri("http://localhost:5678/DemoService/metadata");  
  6.   host.Description.Behaviors.Add(b);  
  7.   host.Opened += delegate{ label1.Text = "服务启动";};  
  8.   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.ServiceModel和System.Runtime.Serialization;

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

  在窗体上放置一个Button和DataGridView,为Button的Click编写代码如下:

 

  1.   DemoServiceClient c = newDemoServiceClient();  
  2.   dataGridView1.DataSource = c.GetMonitorData(); 

 

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

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

  2、添加服务引用

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

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

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

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

 

  1.   ServiceReference1.DemoServiceClient c = newServiceReference1.DemoServiceClient();  
  2.   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项目的启动服务代码处增加一行添加终结点代码:

 

  1.   host = newServiceHost(typeof(DemoService));  
  2.   host.AddServiceEndpoint(typeof(IDemoService), newBasicHttpBinding(), "http://localhost:5678/DemoService");  
  3.   ServiceMetadataBehavior b = newServiceMetadataBehavior();  
  4.   b.HttpGetEnabled = true;  
  5.   b.HttpGetUrl = newUri("http://localhost:5678/DemoService/metadata");  
  6.   host.Description.Behaviors.Add(b);  
  7.   host.AddServiceEndpoint(typeof(IMetadataExchange), newCustomBinding(newHttpTransportBindingElement()), "http://localhost:5678/DemoService/MEX");  
  8.   host.Opened += delegate{ label1.Text = "服务启动";};  
  9.   host.Open(); 

 

  效果同上。

原文链接:http://www.cnblogs.com/Higel/archive/2011/12/28/2303031.html

【编辑推荐】

  1. 5月最新超有趣的免费jQuery插件推荐
  2. 从零开始学习jQuery之管理jQuery包装集
  3. jQuery性能指标和调优
  4. 手把手教你jQuery jqPlot画图插件
  5. 从零开始学习jQuery之万能的选择器
责任编辑:彭凡 来源: 博客园
相关推荐

2010-06-11 16:59:09

DSR路由协议

2009-11-05 10:07:37

WCF设计模式

2009-01-22 10:31:28

2009-10-22 18:06:31

CLR存储过程

2010-06-30 11:21:42

UML业务建模

2010-02-26 14:39:27

WCF服务寄宿

2009-11-06 17:10:34

WCF服务性能计数器

2009-12-08 16:59:52

WCF服务

2009-12-22 15:47:03

WCF服务器证书

2009-12-04 17:47:04

WCF 服务

2010-03-02 14:06:37

WCF服务实例管理模式

2010-03-02 16:28:11

WCF发布订阅

2009-04-07 16:21:35

ASP.NETAJAXWCF

2009-11-05 15:25:36

WCF服务端配置

2009-11-05 10:13:39

WCF框架

2009-11-05 09:43:11

WCF好处

2009-11-05 11:23:08

WCF宿主

2009-11-09 15:58:07

WCF回调方法

2009-11-06 16:48:03

WCF简介

2010-06-30 13:17:12

UML业务建模
点赞
收藏

51CTO技术栈公众号