一、什么是绑定
绑定是预先配置好的信道栈,它代表了服务器与客户端之间的通信约定,每个绑定都会指定了通信所应用到的传输协调、编码等属性。在 Framework3.5中已经包含basicHttpBinding、wsHttpBinding、wsDualHttpBinding、 webHttpBinding、netTcpBinding、netNamedPipeBinding、netMsmqBinding、 netPeerTcpBinding、msmqIntegrationBinding、wsFedrationHttpBinding、 ws2007HttpBinding、ws2007FederationHttpBinding等多种绑定。其中不同的绑定支持不同的传输协议,在消息编 码、传输安全、通讯方式、安全模式、可靠性会话、事务流方面有着不同的属性,能够满足大部分信息通讯的要求。
绑定类名称
传输
编码
消息版本
安全模式
可靠性会话
事务流
BasicHttpBinding
HTTP
文本
SOAP 1.1
无
不支持
WSHttpBinding
SOAP 1.2 WS-Addressing 1.0
消息
禁用
WS-AtomicTransactions
WSDualHttpBinding
启用
WSFederationHttpBinding
NetTcpBinding
TCP
二进制
SOAP 1.2
OleTransactions
NetPeerTcpBinding
P2P
NetNamedPipesBinding
命名管道
NetMsmqBinding
MSMQ
MsmqIntegrationBinding
CustomBinding
自定义
自定义
自定义
二、自定义绑定元素
当预定义的绑定无法满足用户需求时,可以使用CustomBinding类开发自定义绑定,该类存在于 System.ServiceModel.Channels命名空间。用户可以根据需要绑定以下属性: 事务(TransactionFlowBindingElement类)、可靠性会话(ReliableSessionBindingElement 类)、安全( SecurityBindingElement 类)、流安全、单工双工工作模式、信息编码、传输绑定等,其中信息编码和传输绑定元素是自定义绑定的必要属性,其他属性用户可根据需求制定。
传输绑定元素(必要),用户可选其中一种传输绑定模式。
传输信道
传输绑定元素
绑定扩展
配置元素
TCP传输信道
TcpTransportBindingElement
TcpTransportElement
<tcpTransport>
HTTP传输信道
HttpTransportionBindingElement
HttpTransportElement
<httpTransport>
HTTPS传输信道
HttpTransportationBindingElement
HttpTransportElement
MSMQ传输信道
MSMQTransportBindingElement
MSMQTransportElement
<msmqTransport>
MSMQ集成传输信道
MSMQIntegrationBindingElement
MSMQIntegrationBindingElement
<msmqIntegration>
命名管道传输信道
NamedPipeTransportBindingElement
NamedPipeTransportElement
<namedPipeTransport>
P2P传输信道
PeerTransportBindingElement
PeerTransportElement
<peerTransport>
UDP传输信道
UdpTransportBindingElement
UdpTransportElement
<udpTransport>
信息编码(必要),用户可以选择其中一种信息编码形式
1.TextMessageEncodingBindingElement,文本编码
2.BinaryMessageEncodingBindingElement,二进制编码
3.MtomMessageEncodingBindingElement,MOTM编码
流安全绑定元素(可选),用户可以选择其中一种安全绑定形式
1.SslStreamSecurityBindingElement,SSL安全模式
2.WindowsStreamSecurityBindingElement,Window安全模式
通信传输(可选),用户可以选择单工或双工其中一种模式
1.CompositeDuplexBindingElement,双工传输模式
2.OneWayBindingElement,单工传输模式
CustomBinding相当于一个绑定的容器,用户可以向里面加入想要的绑定元素,定制一组适合使用的绑定方式。用户可以分别使用代码、配置文件和绑定扩展类三种方式来开发自定义绑定,下面为大家一一说明。
三、使用代码定制自定义绑定
下面以一个最基本的自定义绑定为例子,简单说明一下如何使用代码来定制绑定,首先新建一个服务契约
1 namespace CustomBinding.Server
2 {
3 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IExampleService”。
4 [ServiceContract(Namespace="Services")]
5 public interface IExampleService
6 {
7 [OperationContract]
8 string HelloWorld(string name);
9 }
10
11 public class ExampleService : IExampleService
12 {
13 public string HelloWorld(string name)
14 {
15 return "Hello " + name;
16 }
17 }
18 }
在服务器端,首先新建一个CustomBinding自定义绑定对象,加入传输绑定元素HttpTransportBindingElement,然后加 入信息编码元素TextMessageEncodingBindingElement(注意,若使用 HttpTransportBindingElement传输方式时,可省略信息编码绑定,那么系统将默认使用 TextMessageEncodingBindingElement编码方式)。最后开放元数据,把服务行为的httpGetEnabled设置为 true。
3 class Program
4 {
5 static void Main(string[] args)
6 {
7 CustomBinding();
8 }
9
10 public static void CustomBinding()
11 {
12 using (ServiceHost host = new ServiceHost(typeof(ExampleService), new Uri("http://localhost:8081/Services")))
13 {
14 //新建一个CustomBinding对象
15 System.ServiceModel.Channels.CustomBinding customBinding = new System.ServiceModel.Channels.CustomBinding();
16 //设置信息编码
17 customBinding.Elements.Add(new TextMessageEncodingBindingElement());
18 //设置传输绑定元素
19 customBinding.Elements.Add(new HttpTransportBindingElement());
20 //绑定服务契约
21 host.AddServiceEndpoint(typeof(IExampleService), customBinding, "CustomService");
22 //开放元数据
23 ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
24 behavior.HttpGetEnabled = true;
25 host.Description.Behaviors.Add(behavior);
26 host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "CustomService/mex");
27 //启动服务
28 host.Open();
29 Console.WriteLine("Service Start!");
30 Console.ReadKey();
31 host.Close();
32 }
33 }
34 }
在客户端添加服务引用,元数据路径“http://localhost:8081/Services/CustomService/mex”,客户端将对 应服务生成wsHttpBinding绑定,并使用TextMessageEncodingBindingElement文本编码元素。最后调用服务测 试,若测试成功,系统将显示测试结果:“Hello Leslie”。
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <system.serviceModel>
4 <bindings>
5 <!--由于自定义绑定是使用HttpTransportBindingElement绑定元素,所以客户端将自动生成wsHttpBinding设置-->
6 <wsHttpBinding>
7 <!--注意绑定元素将对应使用TextMessageEncodingBindingElement信息编码元素-->
8 <binding name="CustomBinding_IExampleService" closeTimeout="00:01:00"
9 openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
10 bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
11 maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
12 messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
13 allowCookies="false">
14 <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
15 maxBytesPerRead="4096" maxNameTableCharCount="16384" />
16 <reliableSession ordered="true" inactivityTimeout="00:10:00"
17 enabled="false" />
18 <security mode="None">
19 <transport clientCredentialType="Windows" proxyCredentialType="None"
20 realm="" />
21 <message clientCredentialType="Windows" negotiateServiceCredential="true" />
22 </security>
23 </binding>
24 </wsHttpBinding>
25 </bindings>
26 <client>
27 <endpoint address="http://localhost:8081/Services/CustomService"
28 binding="wsHttpBinding" bindingConfiguration="CustomBinding_IExampleService"
29 contract="ExampleService.IExampleService" name="CustomBinding_IExampleService" />
30 </client>
31 </system.serviceModel>
32 </configuration>
33
34 namespace CustomBinding.Client
35 {
36 class Program
37 {
38 static void Main(string[] args)
39 {
40 //新建服务对象,调用服务方法
41 using (ExampleService.ExampleServiceClient example = new ExampleService.ExampleServiceClient())
42 {
43 string data=example.HelloWorld("Leslie");
44 Console.WriteLine(data);
45 Console.ReadKey();
46 }
47 }
48 }
49 }
四、使用配置文件设置自定义绑定
3 <system.serviceModel>
4 <services>
5 <!--根据服务契约类的命名空间设置name名称,设置服务行为defaultBehavior-->
6 <service name="CustomBinding.Server.ExampleService" behaviorConfiguration="defaultBehavior">
7 <host>
8 <baseAddresses>
9 <!--绑定服务地址-->
10 <add baseAddress="http://localhost:8082/CustomBinding.Server/ExampleService"/>
11 </baseAddresses>
12 </host>
13 <!--加入自定义绑定-->
14 <endpoint address="" contract="CustomBinding.Server.IExampleService" binding="customBinding"
15 bindingConfiguration="customBinding"/>
16 <!--开放元数据-->
17 <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
18 </service>
19 </services>
20 <behaviors>
21 <serviceBehaviors>
22 <!--设置服务行为defaultBehavior,把httpGetEnabled设置为true-->
23 <behavior name="defaultBehavior">
24 <serviceMetadata httpGetEnabled="true"/>
25 </behavior>
26 </serviceBehaviors>
27 </behaviors>
28 <bindings>
29 <customBinding>
30 <!--设置自定义绑定的属性,把信息编码方式设置为textMessageEncoding文本形式,并把传输通道设置为httpTransport-->
31 <!--把可靠性会话时间设置为30分钟-->
32 <binding name="customBinding">
33 <reliableSession inactivityTimeout="00:30:00"/>
34 <textMessageEncoding/>
35 <httpTransport/>
36 </binding>
37 </customBinding>
38 </bindings>
39 </system.serviceModel>
40 </configuration>
设置好config文件后,启动服务
12 using(ServiceHost host=new ServiceHost(typeof(ExampleService)))
14 Console.WriteLine("Service Start!");
15 host.Open();
16 Console.ReadKey();
17 host.Close();
18 }
19 }
20 }
21 }
在客户端添加服务引用,元数据路径“http://lcoalhost:8082/CustomBinding.Server /ExampleService/mex”,客户端将对应服务生成wsHttpBinding绑定,并使用TextMessageEncoding文本编 码,值得注意的是客户将对应服务器端生成30分钟可靠性会话。
1 <?xml version="1.0" encoding="utf-8" ?>
5 <!--由于自定义绑定是使用HttpTransportBindingElement绑定元素,所以客户端将自动生成wsHttpBinding设置-->
16 <!--注意客户端将对应服务器端生成30分钟的可靠性会话-->
17 <reliableSession ordered="true" inactivityTimeout="00:30:00"
18 enabled="true" />
19 <security mode="None">
20 <transport clientCredentialType="Windows" proxyCredentialType="None"
21 realm="" />
22 <message clientCredentialType="Windows" negotiateServiceCredential="true" />
23 </security>
24 </binding>
25 </wsHttpBinding>
26 </bindings>
27 <client>
28 <endpoint address="http://localhost:8082/CustomBinding.Server/ExampleService"
29 binding="wsHttpBinding" bindingConfiguration="CustomBinding_IExampleService"
30 contract="ExampleService.IExampleService" name="CustomBinding_IExampleService" />
31 </client>
32 </system.serviceModel>
33 </configuration>
34
35 namespace CustomBinding.Client
36 {
37 class Program
38 {
39 static void Main(string[] args)
40 {
41 //新建服务对象,调用服务方法
42 using (ExampleService.ExampleServiceClient example = new ExampleService.ExampleServiceClient())
43 {
44 string data=example.HelloWorld("Leslie");
45 Console.WriteLine(data);
46 Console.ReadKey();
47 }
48 }
49 }
50 }
五、使用绑定扩展类实现自定义绑定
<a href="http://www.cnblogs.com/leslies2/archive/2011/10/14/2195800.html" target="_blank">自定义绑定(下)</a>
<a href="http://www.cnblogs.com/leslies2/archive/2011/01/26/1934163.html">简单的WCF开发实例</a>
<a href="http://www.cnblogs.com/leslies2/archive/2011/01/26/1934889.html">使用AJAX+WCF进行页面开发</a>
<a href="http://www.cnblogs.com/leslies2/archive/2011/08/03/2115091.html">共享数据契约</a>
<a href="http://www.cnblogs.com/leslies2/archive/2011/08/08/2129422.html">可靠性会话功能</a>
本文转自 leslies2 51CTO博客,原文链接:http://blog.51cto.com/79100812/702861