WCF简单教程(2) 聊聊binding

简介:

第二篇:聊聊binding

上一篇构建的WCF程序,binding指定的是basicHttpBinding,这是最基础的通讯方式,基于http,不加密,抓一下包的话,应该是这样的:

 
  1. 发送包: 
  2. POST /wcf HTTP/1.1 
  3. Content-Type: text/xml; charset=utf-8 
  4. SOAPAction: "WCF.Demo/IData/SayHello" 
  5. Host: 127.0.0.1:8080 
  6. Content-Length: 156 
  7. Expect: 100-continue 
  8. Connection: Keep-Alive 
  9.  
  10. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><SayHello xmlns="WCF.Demo"><userName>WCF</userName></SayHello></s:Body></s:Envelope> 
  11.  
  12. -------------------------
  13. 应答包: 
  14. HTTP/1.1 100 Continue 
  15.  
  16. HTTP/1.1 200 OK 
  17. Content-Length: 191 
  18. Content-Type: text/xml; charset=utf-8 
  19. Server: Microsoft-HTTPAPI/2.0 
  20. Date: Mon, 05 Mar 2012 08:45:31 GMT 
  21.  
  22. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><SayHelloResponse xmlns="WCF.Demo"><SayHelloResult>Hello WCF.</SayHelloResult></SayHelloResponse></s:Body></s:Envelope> 

就是SOAP,和WebService是一样的。basicHttpBinding的优势在于通用,又基于http,所以在跨语言的开发中或者是穿透复杂网络环境时占优势,但是效率比较低,因为SOAP的序列化和反序列化比较耗时,传输的数据量也比较大,而且是明文,安全性差。

WCF的binding有很多种,包括:

basicHttpBinding(常用) 符合WSBasicProfile 1.1标准,以提供最大的互操作性
wsHttpBinding(常用)  符合WS-*协议的HTTP通讯,加密 
wsDualHttpBinding  双工http通信,初始信息的接收者不会直接响应发送者,而是可以在一定时间之内传送任意次的响应 
wsFederationBinding  http通信,对服务资源的访问可以根据一个显式确定的凭据提供程序发出的凭据加以控制 
netTcpBinding(常用)  提供网络里WCF软件实体之间的安全可靠高性能的通信 
netNamedPipeBinding  同一台机器上的WCF软件实体之间的安全可靠高性能的通信 
netMsmqBinding  WCF软件实体与其它软件实体间通过MSMQ通信 
msmqIntegrationBinding 软件实体与其它软件实体间通过MSMQ通信 
netPeerTcpBinding  WCF软件实体间通过Windows对等网络技术通信 

内容很多,我们挑wsHttpBinding和netTcpBinding继续研究一下,这两个最有代表性。在上一篇demo的基础上修改一下。

1、服务端

服务端的功能逻辑没有任何变化,只是调整绑定方式,所以只调整App.config:

 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <services>  
  5.       <service name="Server.DataService">  
  6.         <host>  
  7.           <baseAddresses>  
  8.             <add baseAddress="http://localhost:8080/wcf" /> 
  9.             <!-- 此处增加了个baseAddress,注意标头指定的是net.tcp,另外端口不能被占用 --> 
  10.             <add baseAddress="net.tcp://localhost:8081/wcf" /> 
  11.           </baseAddresses>  
  12.         </host>  
  13. <!-- 此处调整了绑定使用wsHttpBinding方式 --> 
  14.         <endpoint address="" binding="wsHttpBinding" contract="Server.IData" /> 
  15.         <!-- 此处增加了一个endpoint,使用netTcpBinding方式,服务契约同样是IData --> 
  16.         <endpoint address="" binding="netTcpBinding" contract="Server.IData" /> 
  17.       </service>  
  18.     </services>  
  19.   </system.serviceModel>  
  20. </configuration>  

与之前相比,增加了一个baseAddress和一个endpoint,另外调整了之前endpoint的绑定方式。现在针对同一个服务契约,有两个endpoint了,但是它们不会冲突,因为两者的网络协议不同,所以wsHttpBinding的会使用http://localhost:8080/wcf的地址,而netTcpBinding的会使用net.tcp://localhost:8081/wcf的地址。

如果同时定义了basicHttpBinding和wsHttpBinding呢?那么它们的address一定不能相同,因为baseAddress已经相同了,adress还一样,就无法访问了。

编译运行一下,然后命令行“netstat -ano”看一下,应该能看到8080和8081都开始监听了。

 
  1. TCP    0.0.0.0:8080        0.0.0.0:0          LISTENING       4 
  2. TCP    0.0.0.0:8081        0.0.0.0:0          LISTENING       692 
  3. TCP    [::]:8080           [::]:0             LISTENING       4 
  4. TCP    [::]:8081           [::]:0             LISTENING       692 

TCP 8081端口的PID显示是我们的服务程序在对外监听。


2、客户端

由于服务端修改了绑定方式,客户端必须要与之匹配,先修改App.config文件:

 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <client>  
  5.       <!-- 此处调整了binding为wsHttpBinding --> 
  6.       <endpoint name="httpDataService" address="http://localhost:8080/wcf" binding="wsHttpBinding" contract="Server.IData" /> 
  7.       <!-- 此处新增了一个endpoint,指定使用netTcpBinding方式 --> 
  8.       <endpoint name="tcpDataService" address="net.tcp://localhost:8081/wcf" binding="netTcpBinding" contract="Server.IData" /> 
  9.     </client>  
  10.   </system.serviceModel>  
  11. </configuration>  

需要指出的是,服务端开放了2种访问方式,客户端不一定也要写2个endpoint,这里是为了测试两种绑定。

调用代码也要做个小修改:

 
  1. using System; 
  2. using System.ServiceModel; 
  3. using System.ServiceModel.Channels; 
  4.  
  5. namespace Client 
  6.     class Program 
  7.     { 
  8.         static void Main(string[] args) 
  9.         { 
  10.          //定义一个http方式的代理,配置使用httpDataService中的定义 
  11.             var httpProxy = new ChannelFactory<Server.IData>("httpDataService").CreateChannel(); 
  12.             Console.WriteLine(httpProxy.SayHello("WCF")); 
  13.             ((IChannel)httpProxy).Close(); 
  14.  
  15.          //定义一个tcp方式的代理,配置使用tcpDataService中的定义 
  16.             var tcpProxy = new ChannelFactory<Server.IData>("tcpDataService").CreateChannel(); 
  17.             Console.WriteLine(tcpProxy.SayHello("WCF")); 
  18.             ((IChannel)tcpProxy).Close(); 
  19.         } 
  20.     } 


编译运行一下,应该能够输出两行 Hello WCF.。

抓包看看:

wsHttpBinding方式的客户端与服务端总共交换了60多个数据包,这是因为双方要先握手交换密钥,另外由于数据加了密,长度也变大了。这里只截第一次交互的数据看看:

 
  1. 发送包: 
  2. POST /wcf HTTP/1.1 
  3. Content-Type: application/soap+xml; charset=utf-8 
  4. Host: 127.0.0.1:8080 
  5. Content-Length: 1106 
  6. Expect: 100-continue 
  7. Connection: Keep-Alive 
  8.  
  9. <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action><a:MessageID>urn:uuid:144b8aeb-6ac1-46f5-8361-957425b827c8</a:MessageID><a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo><a:To s:mustUnderstand="1">http://192.168.90.81:8080/wcf</a:To></s:Header><s:Body><t:RequestSecurityToken Context="uuid-156d27d6-3db7-43ac-b488-12f9ae123861-1" xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust"><t:TokenType>http://schemas.xmlsoap.org/ws/2005/02/sc/sct</t:TokenType><t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType><t:KeySize>256</t:KeySize><t:BinaryExchange ValueType="http://schemas.xmlsoap.org/ws/2005/02/trust/spnego" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">TlRMTVNTUAABAAAAt7IY4gkACQAxAAAACQAJACgAAAAGAbAdAAAAD1RJQU5ZVS1QQ1dPUktHUk9VUA==</t:BinaryExchange></t:RequestSecurityToken></s:Body></s:Envelope> 
  10.  
  11. --------------------- 
  12. 应答包: 
  13. HTTP/1.1 100 Continue 
  14.  
  15. HTTP/1.1 200 OK 
  16. Content-Length: 1044 
  17. Content-Type: application/soap+xml; charset=utf-8 
  18. Server: Microsoft-HTTPAPI/2.0 
  19. Date: Tue, 06 Mar 2012 01:46:24 GMT 
  20.  
  21. <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/Issue</a:Action><a:RelatesTo>urn:uuid:144b8aeb-6ac1-46f5-8361-957425b827c8</a:RelatesTo></s:Header><s:Body><t:RequestSecurityTokenResponse Context="uuid-156d27d6-3db7-43ac-b488-12f9ae123861-1" xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><t:BinaryExchange ValueType="http://schemas.xmlsoap.org/ws/2005/02/trust/spnego" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">TlRMTVNTUAACAAAAEgASADgAAAA1wprik+hmjwEji3X4ciEAAAAAAGgAaABKAAAABgGwHQAAAA9UAEkAQQBOAFkAVQAtAFAAQwACABIAVABJAEEATgBZAFUALQBQAEMAAQASAFQASQBBAE4AWQBVAC0AUABDAAQAEgBUAGkAYQBuAFkAdQAtAFAAQwADABIAVABpAGEAbgBZAHUALQBQAEMABwAIAPm5CvA6+8wBAAAAAA==</t:BinaryExchange></t:RequestSecurityTokenResponse></s:Body></s:Envelope> 

应该是双方交换了一个256位的密钥,反正所有的数据交互都不再是明文的了。

再来看netTcpBinding的这次,还是只截一小段吧:

 
  1. 发送包: 
  2. 00000000  00 01 00 01 02 02 20 6E  65 74 2E 74 63 70 3A 2F   ...... n et.tcp:/  
  3. 00000010  2F 31 39 32 2E 31 36 38  2E 39 30 2E 38 31 3A 38   /192.168 .90.81:8  
  4. 00000020  30 38 31 2F 77 63 66 03  08 09 15 61 70 70 6C 69   081/wcf. ...appli  
  5. 00000030  63 61 74 69 6F 6E 2F 6E  65 67 6F 74 69 61 74 65   cation/n egotiate  
  6. 00000040  16 01 00 00 3A 4E 54 4C  4D 53 53 50 00 01 00 00   ....:NTL MSSP....  
  7. 00000050  00 B7 B2 18 E2 09 00 09  00 31 00 00 00 09 00 09   ........ .1..... 
  8.  
  9. ---------- 
  10. 应答包: 
  11. 00000000  0A 16 01 00 00 B2 4E 54  4C 4D 53 53 50 00 02 00   ......NT LMSSP...  
  12. 00000010  00 00 12 00 12 00 38 00  00 00 35 C2 9A E2 3A D5   ......8. ..5...:.  
  13. 00000020  19 64 33 D4 B9 7C F8 72  21 00 00 00 00 00 68 00   .d3..|.r !.....h.  
  14. 00000030  68 00 4A 00 00 00 06 01  B0 1D 00 00 00 0F 54 00   h.J..... ......T.  
  15. 00000040  49 00 41 00 4E 00 59 00  55 00 2D 00 50 00 43 00   I.A.N.Y. U.-.P.C.  
  16. 00000050  02 00 12 00 54 00 49 00  41 00 4E 00 59 00 55 00   ....T.I. A.N.Y.U.  

从效率上讲,tcp方式要比http方式高得多,同时http方式,basicHttpBinding又要比wsHttpBinding高得多,在实际使用中,大家要看需求来决定使用哪种方式。

OK,下一篇我们看看能不能不用写复杂的配置就发布WCF服务,也以此来加深一下对Host、Contract、Binding、BaseAddress的理解。

 



     本文转自 BoyTNT 51CTO博客,原文链接:http://blog.51cto.com/boytnt/796993,如需转载请自行联系原作者

相关文章
艾伟:[WCF中的Binding模型]之一: Binding模型简介
1. 信道层与服务模型层(Channel Layer and Service Mode Layer) 对于一个分布式应用的开发与设计来说,通信问题是不得不考虑,同时也是最为复杂、最难实现的问题。在过去的若干年中, 微软先后推出了一系列广受欢迎的通信技术, 比如DCOM、Enterprise Service、.NET Remoting、XML Web Service、MSMQ等等。
1019 0
|
安全 网络协议 数据安全/隐私保护
艾伟:[WCF中的Binding模型]之六(完结篇):从绑定元素认识系统预定义绑定
由于绑定对象由一系列有序的绑定元素组成,绑定元素最终决定着信道栈中信道的组成,而信道的组成最终又决定了信道栈对消息进行处理的方式和能力,所有要确定绑定的特性和能力,我们可以通过查看其绑定元素的构成来一窥究竟。
894 0
|
安全 数据安全/隐私保护 Windows
|
安全 数据安全/隐私保护 Windows