一、什么是绑定

绑定是预先配置好的信道栈,它代表了服务器与客户端之间的通信约定,每个绑定都会指定了通信所应用到的传输协调、编码等属性。在 Framework3.5中已经包含basicHttpBinding、wsHttpBinding、wsDualHttpBinding、 webHttpBinding、netTcpBinding、netNamedPipeBinding、netMsmqBinding、 netPeerTcpBinding、msmqIntegrationBinding、wsFedrationHttpBinding、 ws2007HttpBinding、ws2007FederationHttpBinding等多种绑定。其中不同的绑定支持不同的传输协议,在消息编 码、传输安全、通讯方式、安全模式、可靠性会话、事务流方面有着不同的属性,能够满足大部分信息通讯的要求。

绑定类名称传输编码消息版本安全模式可靠性会话事务流
BasicHttpBindingHTTP文本SOAP 1.1不支持不支持
WSHttpBindingHTTP文本SOAP 1.2 WS-Addressing 1.0消息禁用WS-AtomicTransactions
WSDualHttpBindingHTTP文本SOAP 1.2 WS-Addressing 1.0消息启用WS-AtomicTransactions
WSFederationHttpBindingHTTP文本SOAP 1.2 WS-Addressing 1.0消息禁用WS-AtomicTransactions
NetTcpBindingTCP二进制SOAP 1.2传输禁用OleTransactions
NetPeerTcpBindingP2P二进制SOAP 1.2传输不支持不支持
NetNamedPipesBinding命名管道二进制SOAP 1.2传输不支持OleTransactions
NetMsmqBindingMSMQ二进制SOAP 1.2消息不支持不支持
MsmqIntegrationBindingMSMQ不支持不支持传输不支持不支持
CustomBinding自定义 自定义 自定义自定义  自定义自定义

 

二、自定义绑定元素

当预定义的绑定无法满足用户需求时,可以使用CustomBinding类开发自定义绑定,该类存在于 System.ServiceModel.Channels命名空间。用户可以根据需要绑定以下属性: 事务(TransactionFlowBindingElement类)、可靠性会话(ReliableSessionBindingElement 类)、安全( SecurityBindingElement 类)、流安全、单工双工工作模式、信息编码、传输绑定等,其中信息编码和传输绑定元素是自定义绑定的必要属性,其他属性用户可根据需求制定。

  • 传输绑定元素(必要),用户可选其中一种传输绑定模式。
传输信道传输绑定元素绑定扩展配置元素
TCP传输信道TcpTransportBindingElementTcpTransportElement<tcpTransport>
HTTP传输信道HttpTransportionBindingElement  HttpTransportElement  <httpTransport>
HTTPS传输信道HttpTransportationBindingElementHttpTransportElement<httpTransport>
MSMQ传输信道MSMQTransportBindingElementMSMQTransportElement<msmqTransport>
MSMQ集成传输信道MSMQIntegrationBindingElement  MSMQIntegrationBindingElement<msmqIntegration>
命名管道传输信道NamedPipeTransportBindingElementNamedPipeTransportElement  <namedPipeTransport>
P2P传输信道 PeerTransportBindingElementPeerTransportElement<peerTransport>
UDP传输信道UdpTransportBindingElementUdpTransportElement<udpTransport>
  • 信息编码(必要),用户可以选择其中一种信息编码形式

                  1.TextMessageEncodingBindingElement,文本编码

                  2.BinaryMessageEncodingBindingElement,二进制编码

                  3.MtomMessageEncodingBindingElement,MOTM编码

  • 流安全绑定元素(可选),用户可以选择其中一种安全绑定形式

                  1.SslStreamSecurityBindingElement,SSL安全模式

                  2.WindowsStreamSecurityBindingElement,Window安全模式

  • 通信传输(可选),用户可以选择单工或双工其中一种模式

                  1.CompositeDuplexBindingElement,双工传输模式

                  2.OneWayBindingElement,单工传输模式

CustomBinding相当于一个绑定的容器,用户可以向里面加入想要的绑定元素,定制一组适合使用的绑定方式。用户可以分别使用代码、配置文件和绑定扩展类三种方式来开发自定义绑定,下面为大家一一说明。


三、使用代码定制自定义绑定

下面以一个最基本的自定义绑定为例子,简单说明一下如何使用代码来定制绑定,首先新建一个服务契约

 
  
  1.  1 namespace CustomBinding.Server 
  2.  2 { 
  3.  3     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IExampleService”。 
  4.  4     [ServiceContract(Namespace="Services")] 
  5.  5     public interface IExampleService 
  6.  6     { 
  7.  7         [OperationContract] 
  8.  8         string HelloWorld(string name); 
  9.  9     } 
  10. 10    
  11. 11     public class ExampleService : IExampleService 
  12. 12     { 
  13. 13         public string HelloWorld(string name) 
  14. 14         { 
  15. 15             return "Hello " + name; 
  16. 16         } 
  17. 17     } 
  18. 18 } 

在服务器端,首先新建一个CustomBinding自定义绑定对象,加入传输绑定元素HttpTransportBindingElement,然后加 入信息编码元素TextMessageEncodingBindingElement(注意,若使用 HttpTransportBindingElement传输方式时,可省略信息编码绑定,那么系统将默认使用 TextMessageEncodingBindingElement编码方式)。最后开放元数据,把服务行为的httpGetEnabled设置为 true。

 
  
  1.  1 namespace CustomBinding.Server 
  2.  2 { 
  3.  3     class Program 
  4.  4     { 
  5.  5         static void Main(string[] args) 
  6.  6         { 
  7.  7             CustomBinding(); 
  8.  8         } 
  9.  9  
  10. 10         public static void CustomBinding() 
  11. 11         { 
  12. 12             using (ServiceHost host = new ServiceHost(typeof(ExampleService), new Uri("http://localhost:8081/Services"))) 
  13. 13             { 
  14. 14                 //新建一个CustomBinding对象 
  15. 15                 System.ServiceModel.Channels.CustomBinding customBinding = new System.ServiceModel.Channels.CustomBinding(); 
  16. 16                 //设置信息编码 
  17. 17                 customBinding.Elements.Add(new TextMessageEncodingBindingElement()); 
  18. 18                 //设置传输绑定元素 
  19. 19                 customBinding.Elements.Add(new HttpTransportBindingElement()); 
  20. 20                 //绑定服务契约 
  21. 21                 host.AddServiceEndpoint(typeof(IExampleService), customBinding, "CustomService"); 
  22. 22                 //开放元数据 
  23. 23                 ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); 
  24. 24                 behavior.HttpGetEnabled = true
  25. 25                 host.Description.Behaviors.Add(behavior); 
  26. 26                 host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "CustomService/mex"); 
  27. 27                 //启动服务 
  28. 28                 host.Open(); 
  29. 29                 Console.WriteLine("Service Start!"); 
  30. 30                 Console.ReadKey(); 
  31. 31                 host.Close(); 
  32. 32             } 
  33. 33         } 
  34. 34     } 

在客户端添加服务引用,元数据路径“http://localhost:8081/Services/CustomService/mex”,客户端将对 应服务生成wsHttpBinding绑定,并使用TextMessageEncodingBindingElement文本编码元素。最后调用服务测 试,若测试成功,系统将显示测试结果:“Hello Leslie”。

 
  
  1. <?xml version="1.0" encoding="utf-8" ?> 
  2.  2 <configuration> 
  3.  3     <system.serviceModel> 
  4.  4         <bindings> 
  5.  5           <!--由于自定义绑定是使用HttpTransportBindingElement绑定元素,所以客户端将自动生成wsHttpBinding设置--> 
  6.  6             <wsHttpBinding> 
  7.  7               <!--注意绑定元素将对应使用TextMessageEncodingBindingElement信息编码元素--> 
  8.  8                 <binding name="CustomBinding_IExampleService" closeTimeout="00:01:00" 
  9.  9                     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
  10. 10                     bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
  11. 11                     maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
  12. 12                     messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
  13. 13                     allowCookies="false"> 
  14. 14                     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
  15. 15                         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
  16. 16                     <reliableSession ordered="true" inactivityTimeout="00:10:00" 
  17. 17                         enabled="false" /> 
  18. 18                     <security mode="None"> 
  19. 19                         <transport clientCredentialType="Windows" proxyCredentialType="None" 
  20. 20                             realm="" /> 
  21. 21                         <message clientCredentialType="Windows" negotiateServiceCredential="true" /> 
  22. 22                     </security> 
  23. 23                 </binding> 
  24. 24             </wsHttpBinding> 
  25. 25         </bindings> 
  26. 26         <client> 
  27. 27             <endpoint address="http://localhost:8081/Services/CustomService" 
  28. 28                 binding="wsHttpBinding" bindingConfiguration="CustomBinding_IExampleService" 
  29. 29                 contract="ExampleService.IExampleService" name="CustomBinding_IExampleService" /> 
  30. 30         </client> 
  31. 31     </system.serviceModel> 
  32. 32 </configuration> 
  33. 33  
  34. 34 namespace CustomBinding.Client 
  35. 35 { 
  36. 36     class Program 
  37. 37     { 
  38. 38         static void Main(string[] args) 
  39. 39         { 
  40. 40             //新建服务对象,调用服务方法 
  41. 41             using (ExampleService.ExampleServiceClient example = new ExampleService.ExampleServiceClient()) 
  42. 42             { 
  43. 43                 string data=example.HelloWorld("Leslie"); 
  44. 44                 Console.WriteLine(data); 
  45. 45                 Console.ReadKey(); 
  46. 46             } 
  47. 47         } 
  48. 48     } 
  49. 49 } 

 

四、使用配置文件设置自定义绑定

除了使用代码设置自定义绑定以外,您也可以使用配置文件来设置自定义绑定。下面的例子,将介绍一下如何在自定义绑定中设置可靠性会话功能(关于可靠性会话的详细说明,可参考以下文章http://www.cnblogs.com/leslies2/archive/2011/08/08/2129422.html)。 下面依然以上面的例子作为参考,首先在服务器端加入配置文件app.config。加入服务 CustomBinding.Server.ExampleService, 绑定服务行为defaultBehavior,在服务行为中打开httpGetEnabled功能。然后把服务地址设置为 http://lcoalhost:8082/CustomBinding.Server/ExampleService,加入自定义绑定 CustomBinding,绑定契约CustomBinding.Server.IExampleService。最后设置自定义绑定的属性,把可靠性 会话时间设置为30分钟,把信息传送方式设置为textMessageEncoding文本方式,把传输通道设置为httpTransport方式。

 
  
  1. <?xml version="1.0" encoding="utf-8" ?> 
  2.  2 <configuration> 
  3.  3   <system.serviceModel> 
  4.  4     <services> 
  5.  5       <!--根据服务契约类的命名空间设置name名称,设置服务行为defaultBehavior--> 
  6.  6       <service name="CustomBinding.Server.ExampleService" behaviorConfiguration="defaultBehavior"> 
  7.  7         <host> 
  8.  8           <baseAddresses> 
  9.  9             <!--绑定服务地址--> 
  10. 10             <add baseAddress="http://localhost:8082/CustomBinding.Server/ExampleService"/> 
  11. 11           </baseAddresses> 
  12. 12         </host> 
  13. 13         <!--加入自定义绑定--> 
  14. 14         <endpoint address="" contract="CustomBinding.Server.IExampleService" binding="customBinding" 
  15. 15                   bindingConfiguration="customBinding"/> 
  16. 16         <!--开放元数据--> 
  17. 17         <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>  
  18. 18       </service> 
  19. 19     </services> 
  20. 20     <behaviors> 
  21. 21       <serviceBehaviors> 
  22. 22         <!--设置服务行为defaultBehavior,把httpGetEnabled设置为true--> 
  23. 23         <behavior name="defaultBehavior"> 
  24. 24           <serviceMetadata httpGetEnabled="true"/> 
  25. 25         </behavior> 
  26. 26       </serviceBehaviors> 
  27. 27     </behaviors> 
  28. 28     <bindings> 
  29. 29       <customBinding> 
  30. 30         <!--设置自定义绑定的属性,把信息编码方式设置为textMessageEncoding文本形式,并把传输通道设置为httpTransport--> 
  31. 31         <!--把可靠性会话时间设置为30分钟--> 
  32. 32         <binding name="customBinding"> 
  33. 33           <reliableSession inactivityTimeout="00:30:00"/> 
  34. 34           <textMessageEncoding/> 
  35. 35           <httpTransport/>  
  36. 36         </binding> 
  37. 37       </customBinding> 
  38. 38     </bindings> 
  39. 39   </system.serviceModel> 
  40. 40 </configuration> 

设置好config文件后,启动服务

 
  
  1.  1 namespace CustomBinding.Server 
  2.  2 { 
  3.  3     class Program 
  4.  4     { 
  5.  5         static void Main(string[] args) 
  6.  6         { 
  7.  7             CustomBinding(); 
  8.  8         } 
  9.  9  
  10. 10         public static void CustomBinding() 
  11. 11         { 
  12. 12             using(ServiceHost host=new ServiceHost(typeof(ExampleService))) 
  13. 13             { 
  14. 14                 Console.WriteLine("Service Start!"); 
  15. 15                 host.Open(); 
  16. 16                 Console.ReadKey(); 
  17. 17                 host.Close(); 
  18. 18             } 
  19. 19         } 
  20. 20     } 
  21. 21 } 

在客户端添加服务引用,元数据路径“http://lcoalhost:8082/CustomBinding.Server /ExampleService/mex”,客户端将对应服务生成wsHttpBinding绑定,并使用TextMessageEncoding文本编 码,值得注意的是客户将对应服务器端生成30分钟可靠性会话。

 
  
  1.  1 <?xml version="1.0" encoding="utf-8" ?> 
  2.  2 <configuration> 
  3.  3     <system.serviceModel> 
  4.  4         <bindings> 
  5.  5             <!--由于自定义绑定是使用HttpTransportBindingElement绑定元素,所以客户端将自动生成wsHttpBinding设置--> 
  6.  6             <wsHttpBinding> 
  7.  7               <!--注意绑定元素将对应使用TextMessageEncodingBindingElement信息编码元素--> 
  8.  8                 <binding name="CustomBinding_IExampleService" closeTimeout="00:01:00" 
  9.  9                     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
  10. 10                     bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
  11. 11                     maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
  12. 12                     messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
  13. 13                     allowCookies="false"> 
  14. 14                     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
  15. 15                         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
  16. 16                     <!--注意客户端将对应服务器端生成30分钟的可靠性会话--> 
  17. 17                     <reliableSession ordered="true" inactivityTimeout="00:30:00" 
  18. 18                         enabled="true" /> 
  19. 19                     <security mode="None"> 
  20. 20                         <transport clientCredentialType="Windows" proxyCredentialType="None" 
  21. 21                             realm="" /> 
  22. 22                         <message clientCredentialType="Windows" negotiateServiceCredential="true" /> 
  23. 23                     </security> 
  24. 24                 </binding> 
  25. 25             </wsHttpBinding> 
  26. 26         </bindings> 
  27. 27         <client> 
  28. 28             <endpoint address="http://localhost:8082/CustomBinding.Server/ExampleService" 
  29. 29                 binding="wsHttpBinding" bindingConfiguration="CustomBinding_IExampleService" 
  30. 30                 contract="ExampleService.IExampleService" name="CustomBinding_IExampleService" /> 
  31. 31         </client> 
  32. 32     </system.serviceModel> 
  33. 33 </configuration> 
  34. 34  
  35. 35 namespace CustomBinding.Client 
  36. 36 { 
  37. 37     class Program 
  38. 38     { 
  39. 39         static void Main(string[] args) 
  40. 40         { 
  41. 41             //新建服务对象,调用服务方法 
  42. 42             using (ExampleService.ExampleServiceClient example = new ExampleService.ExampleServiceClient()) 
  43. 43             { 
  44. 44                 string data=example.HelloWorld("Leslie"); 
  45. 45                 Console.WriteLine(data); 
  46. 46                 Console.ReadKey(); 
  47. 47             } 
  48. 48         } 
  49. 49     } 
  50. 50 } 

 

五、使用绑定扩展类实现自定义绑定

使用扩展类实现自定义绑定是最灵活,使用最广泛的一种自定义绑定方式,特别适用在大型的分布式系统开发中使用,它可以根据需要实现不同的绑定类型,以适应在各种不同平台上使用。对比起前面两种方法,它的实现方式稍微复杂一些,由于篇幅有限,关于绑定扩展类将在自定义绑定(下)为大家介绍。

 

相关文章

自定义绑定(上)

自定义绑定(下)

简单的WCF开发实例

使用AJAX+WCF进行页面开发

共享数据契约

可靠性会话功能

对JAVA与.NET开发有兴趣的朋友欢迎加入QQ群:162338858 点击这里加入此群