basichttpbinding和wshttpbinding是两个基于SOAP的绑定,与REST有很大的不同。 另一方面,您不能只使用浏览器浏览到wshttpbinding端点并查看XML(例如这种绑定的服务如果通过形如http://localhost:端口/testservice.svc的地址访问,将会报http400 basichttpbinding和wshttpbinding的区别如下: basichttpbinding是非常基本的绑定-soap 1.1,在安全性方面不多,在功能方面不多,但与现有的任何SOAP 客户机都兼容——>互操作性好,功能和安全性差 wshttpbinding是一个全面的绑定,它支持大量的ws-*功能和标准-它有更多的安全功能,您可以使用会话连接,您可以使用可靠的消息传递,您可以使用事务控制 ,您可以使用流式处理大数据,但wshttpbinding也有点“笨重”并且当你的消息在网络中传输时,会有很多开销。
接下来我们来进一步分析另外三个基于HTTP的绑定,即WSHttpBinding、WS2007HttpBinding和WSDualHttpBinding。 二、WSHttpBinding/WS2007HttpBinding 接下来我们再来讨论一下WSHttpBinding和WS2007HttpBinding。 WSHttpBinding的Security属性返回的是一个WSHttpSecurity类型的对象。由于WS2007HttpBinding派生自WSHttpBinding,所以它直接将该属性继承下来。 所不同的是,WSHttpBinding和WS2007HttpBinding默认情况下采用Windows凭证。 WSHttpBinding和WS2007HttpBinding安全相关的应用编程接口的定义反映在如下的代码片断中。
服务端配置 <system.serviceModel> <bindings> <wsHttpBinding> <binding name ="<em>WsHttpBinding</em>_IService > <binding name ="WsHttpBinding_IService" maxReceivedMessageSize="370000" receiveTimeout="00: wsHttpBinding的Message安全模式,客户端凭据默认为Windows 运行客户端,抛出异常: ? 2 Message安全模式(本机调试) wsHttpBinding的Message安全模式,客户端凭据默认为Windows。 抛出异常的原因是,wsHttpBinding的Message安全模式不支持https协议,改为http协议则正常。 但是,当服务端的客户端凭据配置与客户端不一致时,也可以正常执行,并获得正常的结果。
web.config中的wcf配置节,做如下处理: <client> <endpoint address="http://localhost:8001/Astro/" binding="<em>wsHttpBinding</em> " bindingConfiguration="<em>WSHttpBinding</em>_IAstroService" contract="IAstroService" name=" " bindingConfiguration="<em>WSHttpBinding</em>_IAstroService" contract="IAstroService" name=" identity> </endpoint> <endpoint address="http://localhost:8003/Astro/" binding="<em>wsHttpBinding</em> " bindingConfiguration="<em>WSHttpBinding</em>_IAstroService" contract="IAstroService" name="
在默认的App.config中,使用的是WCF Framework定义好的wsHttpBinding默认配置,所以看不到binding配置节。 ? 配置节展开如下图: ? WSHttpBinding: 比 BasicHttpBinding 更加安全,通常用于 non-duplex 服务通讯。 WSDualHttpBinding: 和 WSHttpBinding 相比,它支持 duplex 类型的服务。 " bindingConfiguration="WSHttpBinding_IService" contract="WcfSvc1.IService" name="WSHttpBinding_IService " bindingConfiguration="WSHttpBinding_IService1" contract="WcfSvc2.IService" name="WSHttpBinding_IService1
> <binding name="MTMOBinding" messageEncoding="Mtom"> </wsHttpBinding> </bindings> </service> 同样客户端的配置如 : <client> <endpoint address="http://localhost:5504/WebSite2/ISendStreamService" binding="<em>wsHttpBinding</em> " bindingConfiguration="<em>WSHttpBinding</em>_ISendStreamService" contract="ServiceReference1.ISendStreamService " name="<em>WSHttpBinding</em>_ISendStreamService"> </endpoint> </client> <bindings> <wsHttpBinding> <binding name="<em>WSHttpBinding</em>_ISendStreamService" messageEncoding="Mtom" textEncoding="utf-8" > </binding> </wsHttpBinding
一、系统预定义绑定对不同安全模式的支持 下面的表格表示系统预定义绑定对不同安全模式的支持(WSHttpBinding与WS2007HttpBinding具有相同的安全模式支持策略,这里仅仅列出了WSHttpBinding BasicHttpBinding WSHttpBinding WSDualHttpBinding NetNamedPipeBinding NetTcpBinding NetMsmqBinding None BasicHttpBinding WSHttpBinding WSDualHttpBinding NetNamedPipeBinding NetTcpBinding NetMsmqBinding None BasicHttpBinding WSHttpBinding WSDualHttpBinding NetNamedPipeBinding NetTcpBinding NetMsmqBinding None
下面我们来试验一下使用wsHttpBinding 先改配置文件的endpoint节点 <endpoint address="" binding="<em>wsHttpBinding</em>" contract="CalculatorService.ICalculatorService "> 然后在wsHttpBinding中启用reliableSession <wsHttpBinding > <binding> <security mode ="None"/> <reliableSession enabled ="true"/> </binding> </wsHttpBinding> host " name="WSHttpBinding_ICalculatorService"> 然后运行client,先试验一下正常的情况: ? 究其原因,是因为我们在wsHttpBinding中启用了reliableSession,当服务端有未经处理异常时,就会破坏传输的channel,使之进入Faulted 状态。
> <binding name="<em>wsHttpBinding</em>_MyService"> <security mode="Transport"> <transport clientCredentialType " bindingConfiguration="<em>wsHttpBinding</em>_MyService" contract="WebServer.IMyService" > > <binding name="<em>WSHttpBinding</em>_IMyService" closeTimeout="00:01:00" openTimeout= > </bindings> <client> <endpoint address="https://jimmycntvs/MyService.svc" binding="<em>wsHttpBinding</em> name="WSHttpBinding_IMyService" behaviorConfiguration="NewBehavior"> <identity> <dns value="jimmycntvs
5: <services> 6: <service name="CalculatorService"> 7: <endpoint binding="<em>wsHttpBinding</em> address="http://localhost/AspCompatibleServices/CalculatorService.svc" 6: binding="<em>wsHttpBinding</em> 对于WCF来说,Cookie传递能够通过Binding来控制,对于<em>WsHttpBinding</em>来说,默认情况下并不允许Cookie的传递。 我们可以通过<em>WsHttpBinding</em>的AllowCookies来控制是否允许传递Cookie,该属性可以通过配置进行设置。为此,我们对客户端的配置进行了如下的修改。 > 11: <binding name="CookieAllowableBinding" allowCookies="true"/> 12: </wsHttpBinding
二、 WsHttpBinding 我们通过与BasicHttpBinding的方式来分析WsHttpBinding,先通过下面的方式列出在默认条件下(通过默认的构造函数创建WsHttpBinding对象 WsHttpBinding在默认的情况下就提供了对基于消息安全的支持,此外WsHttpBinding仍然提供基于HTTPS的传输安全。 (string[] args) 4: { 5: WsHttpBinding binding = new WsHttpBinding(SecurityMode.Transport 下面的代码中,我们通过另一个构造函数设定WsHttpBinding对可靠会话的支持(第二个参数代表是否支持可靠会话)。 所以从互操作角度讲,WsHttpBinding可以和满足这些标准的Web Service进行互操作。
address="http://localhost:8700/Design_Time_Addresses/SinoSZJS.WebWCF/DocumentWriteService/" binding="wsHttpBinding " bindingConfiguration="WSHttpBinding_ICommonBinding" contract="DocumentWriteService.IDocumentWriteService address="http://localhost:8700/Design_Time_Addresses/SinoSZJS.WebWCF/DocumentWriteService/" binding="wsHttpBinding " bindingConfiguration="WSHttpBinding_ICommonBinding" contract="DocumentWriteService.IDocumentWriteService " bindingConfiguration="WSHttpBinding_ICommonBinding" contract="DocumentWriteService.IDocumentWriteService
本例使用我们熟悉的计算服务例子,在服务寄宿的时候采用WsHttpBinding,下面是客户端程序。 1: Binding binding = new WsHttpBinding(); EndpointAddress address = new EndpointAddress("http://127.0.0.1 以上面例子使用的WsHttpBinding为例,只有WsHttpBinding的安全(Security)或可靠会话(Reliable Session)开启的情况下,创建的信道才具有会话的特性,否则创建出来的信道是不能支持信道的 在默认的情况下,WsHttpBinding的安全模式(SecurityMode)为基于消息的安全,所以创建出来的信道自动被赋予了会话的特性。 ,WsHttpBinding也须要进行相同的设置)。
FirstServiceBehavior"> <endpoint contract="Stone.Address.Contract01.IFirstContract" binding="<em>wsHttpBinding</em> 比如:用http://和net.tcp://作为前缀的基地址就会与基于http协议绑定的BasicHttpBinding、<em>WSHttpBinding</em>和NetTcpBinding的绑定类型相匹配。 " bindingConfiguration="<em>WSHttpBinding</em>_Stone.FirstService" contract="FirstService.StoneFirstService " name="<em>WSHttpBinding</em>_Stone.FirstService"> </endpoint> </client> 逻辑地址与物理地址的分离 终结点地址类型 EndpointAddress ="CalculatorService" address="http://127.0.0.1:3721/CalculatorService" binding="wsHttpBinding
services> <service name="CalculatorService.CalculatorService"> <endpoint address="" binding="<em>wsHttpBinding</em> binding的值是<em>wsHttpBinding</em>,后续会详细讲。 按照工作中实际的使用情况,用的比较多的是<em>wsHttpBinding</em>。 Thank you!
此绑定使用HTTP作为传输协议,并使用文本/XML作为默认的消息编码 HTTP/HTTPS Text,MTOM WSHttpBinding <wsHttpBinding> 一个安全且可互操作的绑定,适合于非双工服务约定 各系统绑定所支持的功能 绑定名称 传输性安全 消息级安全 WS*兼容性 WS*事务支持 持久可靠消息传送 可靠会话 性能 请求/响应 单向 双工 basicHttpBing √ √ √ 良好 √ √ wsHttpBinding
接下来我们主要讨论当我们采用基于HTTP绑定——WSHttpBinding(或者是WS2007HttpBinding)和WSDualHttpBinding)时,实现可靠会话所进行的通信方式。 一、WSHttpBinding V.S. WSDualHttpBinding 如果采用WSHttpBinding,最终创建的是一条从客户端到服务端的HTTP通道。 之所以采用如上的方式的根本目的在于,WSHttpBinding创建的传输层通道是从客户端到服务端的一条HTTP连接。 图2 可靠会话基于通过WSDualHttpBinding创建的双通道的消息交换 我们也可以从另外一种视角来看WSHttpBinding和WSDualHttpBinding对可靠会话的不同实现方式。 对于WSHttpBinding创建的单向信道来说,客户端对于服务端是一个不可寻址(Non-Addressable)的终结点。
5: <services> 6: <service name="CalculatorService"> 7: <endpoint binding="<em>wsHttpBinding</em> address="http://localhost/AspCompatibleServices/CalculatorService.svc" 6: binding="<em>wsHttpBinding</em> 对于WCF来说,Cookie传递能够通过Binding来控制,对于<em>WsHttpBinding</em>来说,默认情况下并不允许Cookie的传递。 我们可以通过<em>WsHttpBinding</em>的AllowCookies来控制是否允许传递Cookie,该属性可以通过配置进行设置。为此,我们对客户端的配置进行了如下的修改。 > 11: <binding name="CookieAllowableBinding" allowCookies="true"/> 12: </wsHttpBinding
此处选用本地Ip地址,端口号为666,ok主机基地址设置完毕,对应host节点中的baseadress节点中的配置 (8)、修改终结点中的binding属性 修改默认终结点的绑定类型为wsHttpBinding httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <wsHttpBinding > <binding name="mybinding" /> </wsHttpBinding> </bindings> <services> <service Service.Service"> <endpoint address="" behaviorConfiguration="endpointBehavior" binding="<em>wsHttpBinding</em>
using (ChannelFactory<ICalculator> channelFatory = new ChannelFactory<ICalculator>(new WSHttpBinding 由于上面的例子中,我们采用了WsHttpBinding,所以在默认条件下创建的信道(Channel)是会话信道(Sessionful Channel)。 对于WsHttpBinding在如下两种情况下下具有创建会话信道的能力: 采用任何一种非None的SecurityMode 采用ReliableSession 再默认的情况下,WsHttpBinding using (ChannelFactory<ICalculator> channelFatory = new ChannelFactory<ICalculator>(new WSHttpBinding using (ChannelFactory<ICalculator> channelFatory = new ChannelFactory<ICalculator>(new WSHttpBinding