WCF源码(绑定)
原创
©著作权归作者所有:来自51CTO博客作者wx636b6259489d3的原创作品,请联系作者获取转载授权,否则将追究法律责任
WCF中指定了通信细节,主要包含协议、编码、传输
1、BasicHttpBinding
这种绑定适用于与符合 WS-Basic Profile 的 Web 服务(例如基于 ASP.NET Web 服务 (ASMX) 的服务)进行的通信。此绑定使用 HTTP 作为传输协议,并使用文本/XML 作为默认的消息编码。
basicHttpBinding的默认安全模式是None,即没有任何安全设置,消息都以明文传送,对客户端也不进行验证。
但是basicHttpBinding绑定可以实现安全传输,也可以通过传输层和消息层来保证消息的安全性。
basicHttpBinding设置为Transport安全模式,传输层的安全是使用IIS的安全机制,比如基本身份验证、集成windows验证、SSL安全通道等等。
basicHttpBinding设置为Message安全模式,消息层使用WS-Security保证消息的安全性,Message模式只支持客户端Certificate验证。
代码编写:
Uri http = new Uri("http://localhost:8080/Service1");
sh = new ServiceHost(typeof(Service1), http);
BasicHttpBinding bhb = new BasicHttpBinding();
ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
sh.Description.Behaviors.Add(mBehave);
sh.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
sh.AddServiceEndpoint(typeof(IService1), bhb, http);
配置文件中配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="BasicHttpBindingServiceLib.Service1" behaviorConfiguration="DerivativesCalculatorService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Service1"/>
</baseAddresses>
</host>
<endpoint address="" name="BasicHttpBindingServiceLib_IService1"
binding="basicHttpBinding" contract="BasicHttpBindingServiceLib.IService1">
</endpoint>
</service>
</services>
<behaviors>
<!--表示就用在服务上的行为被修改,表示在服务在响应Http Get的访问时,生成自己的元数据-->
<serviceBehaviors>
<behavior name="DerivativesCalculatorService">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
2、WSHttpBinding
3、WSualHttpBinding
4、WSFederationHttpBinding
5、NetTcpBinding
6、NetNamedPipeBinding
7、NetPeerTcpBinding
8、MsmgIntegrationBinding