天天看點

WCF源碼(綁定)

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