前面一篇文章《WCF 学习总结1 -- 简单实例》一股脑儿展示了几种WCF部署方式,其中配置文件(App.config/Web.config)都是IDE自动生成,省去了我们不少功夫。现在回过头来看看IDE提供的Wcf Service Library项目模板中的默认服务端配置文件——App.config里面究竟有什么秘密。

服务端的配置文件主要是对services、bindings、behaviors的配置。在默认的App.config中,使用的是WCF Framework定义好的wsHttpBinding默认配置,所以看不到binding配置节。

配置节展开如下图:

BTW: "元数据端点”通过WS-MetadataExchange帮我们实现了对服务的描述,提供了WSDL,启动Host之后我们可以通过http://localhost:8732/Design_Time_Addresses/WcfServiceLib/Service1/?wsdl 查看到公开的服务描述。

配置节展开如下图:

关于WCF中的地址和绑定,需要补充一下。

WCF中支持的传输协议包括HTTP、TCP、Peer network(对等网)、IPC(基于命名管道的内部进程通信)以及MSMQ(微软消息队列),每个协议对应一个地址类型:

  • HTTP地址:http://localhost:8080/
  • TCP地址: net.tcp://localhost:8080/
  • IPC地址: net.pipe://localhost/  (适用于跨进程,不能使用于不同机器间)
  • MSMQ地址: net.msmq://localhost/
  • 对等网地址: net.p2p://localhost/

WCF中提供的绑定有:

  • BasicHttpBinding: 最简单的绑定类型,通常用于 Web Services。使用 HTTP 协议,Text/XML 编码方式。
  • WSHttpBinding: 比 BasicHttpBinding 更加安全,通常用于 non-duplex 服务通讯。
  • WSDualHttpBinding: 和 WSHttpBinding 相比,它支持 duplex 类型的服务。
  • WSFederationHttpBinding: 支持 WS-Federation 安全通讯协议。
  • NetTcpBinding: 效率最高,安全的跨机器通讯方式。
  • NetNamedPipeBinding: 安全、可靠、高效的单机服务通讯方式。
  • NetMsmqBinding: 使用消息队列在不同机器间进行通讯。
  • NetPeerTcpBinding: 使用 P2P 协议在多机器间通讯。
  • MsmqIntegrationBinding: 使用现有的消息队列系统进行跨机器通讯。如 MSMQ。

    ------ 弱弱的分隔线 -----

OK,有了上面的基础,就让WCF风暴来的猛烈些吧。做一个多服务,多端点的示例。

1.WcfServiceLib 代码:

  1. [ServiceContract]  
  2. public interface IService  
  3. {  
  4.     [OperationContract]  
  5.     string GetMessage();  
  6. }  
  7. public class Service1 : IService  
  8. {  
  9.     public string GetMessage()  
  10.     {  
  11.         var address = OperationContext.Current.Channel.LocalAddress.ToString();  
  12.         return string.Format("From Server1: Hello Client at [{0}]", address);   
  13.     }  
  14. }  
  15. public class Service2 : IService  
  16. {  
  17.     public string GetMessage()  
  18.     {  
  19.         var address = OperationContext.Current.Channel.LocalAddress.ToString();  
  20.         return string.Format("来自 Service2: 好 Client at [{0}]", address);  
  21.     }  
  22. }  

2.WcfConsoleHost 代码:

  1. static void Main(string[] args)  
  2. {  
  3.     ServiceHost host1 = new ServiceHost(typeof(WcfServiceLib.Service1));  
  4.     host1.Open();  
  5.     Console.WriteLine("Server1 Opened!");  
  6.     ServiceHost host2 = new ServiceHost(typeof(WcfServiceLib.Service2));  
  7.     host2.Open();  
  8.     Console.WriteLine("Server2 Opened!");  
  9.     Console.Read();  
  10. }  

3.服务端配置文件:
 

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.web>  
  4.     <compilation debug="true" />  
  5.   </system.web>  
  6.   <system.serviceModel>  
  7.     <services>  
  8.       <service name="WcfServiceLib.Service1">  
  9.         <host>  
  10.           <baseAddresses>  
  11.             <add baseAddress = "http://localhost:9999/WcfStudy3/Service1" />  
  12.             <add baseAddress = "net.tcp://localhost:8888/WcfStudy3/Service1" />  
  13.           </baseAddresses>  
  14.         </host>  
  15.         <endpoint address ="serviceEN_1" binding="wsHttpBinding" contract="WcfServiceLib.IService" />  
  16.         <endpoint address ="serviceEN_2" binding="mexTcpBinding" contract="WcfServiceLib.IService" />  
  17.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>  
  18.       </service>  
  19.       <service name="WcfServiceLib.Service2">  
  20.         <host>  
  21.           <baseAddresses>  
  22.             <add baseAddress = "http://localhost:9999/WcfStudy3/Service2" />  
  23.             <add baseAddress = "net.tcp://localhost:8888/WcfStudy3/Service2" />  
  24.           </baseAddresses>  
  25.         </host>  
  26.         <endpoint address ="serviceCH_1" binding="wsHttpBinding" contract="WcfServiceLib.IService" />  
  27.         <endpoint address ="serviceCH_2" binding="mexTcpBinding" contract="WcfServiceLib.IService" />  
  28.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>  
  29.       </service>  
  30.     </services>  
  31.     <behaviors>  
  32.       <serviceBehaviors>  
  33.         <behavior>  
  34.           <serviceMetadata httpGetEnabled="True"/>  
  35.           <serviceDebug includeExceptionDetailInFaults="true" />  
  36.         </behavior>  
  37.       </serviceBehaviors>  
  38.     </behaviors>  
  39.   </system.serviceModel>  
  40. </configuration>  

4. 启动Host,在Client工程中添加Service Reference 
因为有两个Service,所以要添加两次。 
(1) WcfSvc1(Url:http://localhost:9999/WcfStudy3/Service1

(2) WcfSvc2(Url:http://localhost:9999/WcfStudy3/Service2) 图略

5. 客户端配置文件: 配置节中,生成了4个Endpoint,分别对应服务端的4个Endpoint。通过 
name属性区别。

  1. <client>
  2. <endpoint address="http://localhost:9999/WcfStudy3/Service1/serviceEN_1"
  3. binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"
  4. contract="WcfSvc1.IService" name="WSHttpBinding_IService">
  5. </endpoint>
  6. <endpoint address="net.tcp://localhost:8888/WcfStudy3/Service1/serviceEN_2"
  7. binding="netTcpBinding" bindingConfiguration="MetadataExchangeTcpBinding_IService"
  8. contract="WcfSvc1.IService" name="MetadataExchangeTcpBinding_IService" />
  9. <endpoint address="http://localhost:9999/WcfStudy3/Service2/serviceCH_1"
  10. binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
  11. contract="WcfSvc2.IService" name="WSHttpBinding_IService1">
  12. </endpoint>
  13. <endpoint address="net.tcp://localhost:8888/WcfStudy3/Service2/serviceCH_2"
  14. binding="netTcpBinding" bindingConfiguration="MetadataExchangeTcpBinding_IService1"
  15. contract="WcfSvc2.IService" name="MetadataExchangeTcpBinding_IService1" />
  16. </client>

6. 客户端代码:

  1. static void Main(string[] args)  
  2. {  
  3.     Console.WriteLine("------------");  
  4.     WcfSvc1.ServiceClient client1_1 = new WcfSvc1.ServiceClient("WSHttpBinding_IService");  
  5.     Console.WriteLine(client1_1.GetMessage());  
  6.     Console.WriteLine("------------");  
  7.     WcfSvc1.ServiceClient client1_2 = new WcfSvc1.ServiceClient("MetadataExchangeTcpBinding_IService");  
  8.     Console.WriteLine(client1_2.GetMessage());  
  9.     Console.WriteLine("------------");  
  10.     WcfSvc2.ServiceClient client2_1 = new WcfSvc2.ServiceClient("WSHttpBinding_IService1");  
  11.     Console.WriteLine(client2_1.GetMessage());  
  12.     Console.WriteLine("------------");  
  13.     WcfSvc2.ServiceClient client2_2 = new WcfSvc2.ServiceClient("MetadataExchangeTcpBinding_IService1");  
  14.     Console.WriteLine(client2_2.GetMessage());  
  15.     Console.Read();  
  16. }  

7.运行结果:

有人会问,那么生成完的配置文件都要一个个手动修改吗?答案当然不是,VS已经为我们准备了WCF配置工具:IDE > Tools > WCF Service Configuration Editor 。 关于工具的使用,大家可以看这里: http://www.rainsts.net/article.asp?id=441

上面的示例代码,请猛击这里下载

WCF 学习总结2 -- 配置WCF的更多相关文章

  1. WCF学习第二篇:WCF 配置架构。这有助于对wcf配置的理解和记忆

    使用 Windows Communication Foundation (WCF) 配置元素,您可以配置 WCF 服务和客户端应用程序. 可以使用配置编辑器工具 (SvcConfigEditor.ex ...

  2. WCF学习笔记(1)——Hello WCF

    1.什么是WCF Windows Communication Foundation(WCF)是一个面向服务(SOA)的通讯框架,作为.NET Framework 3.0的重要组成部分于2006年正式发 ...

  3. WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】

    http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...

  4. WCF学习系列一【WCF Interview Questions-Part 1 翻译系列】

    http://www.topwcftutorials.net/2012/08/wcf-faqs-part1.html WCF Interview Questions – Part 1 This WCF ...

  5. WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】

    http://www.topwcftutorials.net/2012/10/wcf-faqs-part3.html WCF Interview Questions – Part 3 This WCF ...

  6. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

  7. [WCF学习笔记] 我的WCF之旅(1):创建一个简单的WCF程序

    近日学习WCF,找了很多资料,终于找到了Artech这个不错的系列.希望能从中有所收获. 本文用于记录在学习和实践WCF过程中遇到的各种基础问题以及解决方法,以供日后回顾翻阅.可能这些问题都很基础,可 ...

  8. WCF学习——构建第二个WCF应用程序(五)

    一.创建数据服务 1.在“解决方案资源管理器”中,使用鼠标左键选中“WcfService”项目,然后在菜单栏上,依次选择“项目”.“添加新项”. 2.在“添加新项”对话框中,选择“Web”节点,然后选 ...

  9. WCF学习——构建第二个WCF应用程序(六)

    一.创建客户端应用程序 若要创建客户端应用程序,你将另外添加一个项目,添加对该项目的服务引用,配置数据源,并创建一个用户界面以显示服务中的数据.若要创建客户端应用程序,你将另外添加一个项目,添加对该项 ...

随机推荐

  1. Oracle分区表做跨分区查询

    问:有一张大表,其中按时间字段(TIME_ID)进行表分区(按季度分区),但是如果业务人员做跨季度的大批量数据的查询时,未能走TIME_ID分区索引,导致全表扫描.此种情况该如何处理? 示例解析: 1 ...

  2. (转)《深入理解java虚拟机》学习笔记3——垃圾回收算法

    Java虚拟机的内存区域中,程序计数器.虚拟机栈和本地方法栈三个区域是线程私有的,随线程生而生,随线程灭而灭:栈中的栈帧随着方法的进入和退出而进行入栈和出栈操作,每个栈帧中分配多少内存基本上是在类结构 ...

  3. EXTJS 资料 combo 点一次触发一次

    { xtype: 'combo', id: 'ApplyToModel', name: 'ApplyToModel', store: comStoreApplyToModel, valueField: ...

  4. Hadoop-2.2.0 + Hbase-0.96.2 + Hive-0.13.1(转)

    From:http://www.itnose.net/detail/6065872.html # 需要软件 Hadoop-2.2.0(目前Apache官网最新的Stable版本) Hbase-0.96 ...

  5. [搜片神器]DHT后台管理程序数据库流程设计优化学习交流

    谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器: sosobt.com 大家可以给提点意见... 服务器在抓取和处理同时进行,所以访问速度慢是有些的,特别是搜索速度通过SQL的like来查 ...

  6. 为Dapper编写一个类似于EF的Map配置类

    引言 最近在用Dapper处理Sqlite.映射模型的时候不喜欢用Attribute配置,希望用类似EF的Map来配置,所以粗略的实现了一个. 实现 首先是主体的配置辅助类型: using Syste ...

  7. [转载]Jquery Form插件表单参数

    表单插件API提供了几个方法,让你轻松管理表单数据和进行表单提交. ajaxForm增 加所有需要的事件监听器,为AJAX提交表单做好准备.ajaxForm不能提交表单.在document的ready ...

  8. [转载]JQuery的Ajax跨域请求的解决方案

    今天在项目中需要做远程数据加载并渲染页面,直到开发阶段才意识到ajax跨域请求的问题,隐约记得Jquery有提过一个ajax跨域请求的解决方式,于是即刻翻出Jquery的API出来研究,发现JQuer ...

  9. Tiny6410 设备驱动之helloworld

    在自己的工作目录下建立helloworld_driver.c #include <linux/init.h> #include <linux/module.h> //代码遵守的 ...

  10. Visual C++ 6.0静态、动态链接库

    1.什么是静态连接库,什么是动态链接库          静态链接库与动态链接库都是共享代码的方式,如果采用静态链接库,则无论你愿不愿意,lib 中的指令都全部被直接包含在最终生成的 EXE 文件中了 ...