上一章我們介紹了WCF綁定的基本知識,這一章我們通過一個DEMO來介紹如何建立一個自訂綁定(CustomBinding)的程式。從本章起,我們假定讀者對WCF一有定的瞭解,DEMO建立的過程介紹將省略一些簡單的步驟。自訂綁定(CustomBinding)與前面介紹的WCF程式的區別主要在於通過修改設定檔來達到建立自訂綁定(CustomBinding)的目的。
開發環境:Visual Studio 2010 + Net Framework 4.0 。
1、建立一個WCF Service,主要代碼如下:
介面代碼
[ServiceContract(Namespace = "http://schemas.xinhaijulan.com/demos/CustomBinding")]
public interface IHelloWCFService
{
[OperationContract]
string HelloWCF(string inputMsg);
}
實作類別代碼
public class HelloWCFService:IHelloWCFService
{
public string HelloWCF(string inputMsg)
{
return "The message returned from server:" + inputMsg;
}
}
2、修改App.config檔案:
對於WCF Service項目來說我們可以通過組態工具來進行修改App.config。
2.1、右鍵點擊App.config,選中Edit WCF Configuration,如:
2.2、添加自訂繫結項目,如:
2.3、修改EndPoint,採用customBinding的方式,修改Binding和BindingConfiguration,如:
2.4、關閉組態工具,儲存,自訂綁定的設定檔修改完畢。
3、添加用戶端項目,並添加對CustomBindingServer的引用,主要代碼如下:
用戶端代碼
static void Main(string[] args)
{
using (CustomBindingServer.HelloWCFServiceClient client = new CustomBindingServer.HelloWCFServiceClient())
{
Console.WriteLine("Please input some keys:");
string inputMsg = Console.ReadLine();
Console.WriteLine("The sever will return some message below:");
Console.WriteLine(client.HelloWCF(inputMsg));
}
Console.ReadLine();
}
4、此時我們可以開啟用戶端的app.config檔案,可以看到在此檔案中自動產生的配置方式為我們服務端配置的customBinding方式,與服務端是一致的,如下:
用戶端產生的設定檔
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_IHelloWCFService">
<reliableSession acknowledgementInterval="00:00:00.2000000" flowControlEnabled="true"
inactivityTimeout="00:10:00" maxPendingChannels="4" maxRetryCount="8"
maxTransferWindowSize="8" ordered="true" reliableMessagingVersion="Default" />
<binaryMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
maxSessionSize="2048">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binaryMessageEncoding>
<httpTransport manualAddressing="false" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
useDefaultWebProxy="true" />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://localhost:8732/Design_Time_Addresses/CustomBindingServer/HelloWCFService/"
binding="customBinding" bindingConfiguration="CustomBinding_IHelloWCFService"
contract="CustomBindingServer.IHelloWCFService" name="CustomBinding_IHelloWCFService" />
</client>
</system.serviceModel>
</configuration>
5、設定用戶端為啟動項,F5運行,我們將看到此DEMO正常運行,輸出如下:
Please input some keys:
You are a good boy!
The sever will return some message below:
The message returned from server:You are a good boy!
至此,自訂綁定(CustomBinding)介紹完畢。
點擊下載DEMO。
作者:心海巨瀾
出處:http://xinhaijulan.cnblogs.com
著作權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連,否則保留追究法律責任的權利。