經過一整天的折騰,總算對手動配置WCF有些感覺了,於是寫篇博文記錄一下心得。
根據蔣金楠老師的博文所說的, WCF的終結點有三個要素組成,分別是地址(Address)、綁定(Binding)和契約(Contract),簡記可寫成Endpoint = ABC。
地址:地址決定了服務的位置,解決了服務尋址的問題。
綁定:綁定實現了通信的所有細節,包括網絡傳輸、消息編碼,以及其他為實現某種功能對消息進行的相應處理。綁定的類型包括BasicHttpBinding、WsHttpBinding、NetTcpBinding等。
契約:契約是對服務操作的抽象,也是對消息交換模式以及消息結構的定義。
以上這些內容摘抄自蔣老師的博文。理解的這些對配置WCF很有幫助。
那下面就一步步來配置一個WCF。
首先是服務端,
一個WCF的核心是終結點,那么先把終結點寫列出來,
<services> <service name="BLL.Logic" behaviorConfiguration="te"> <host> <baseAddresses> <add baseAddress="http://localhost:9091/logicService"/> </baseAddresses> </host> <endpoint address="" binding="ws2007HttpBinding" contract="BLL.ILogic" bindingConfiguration="transportWS2007HttpBinding" /> </service> </services>
從<endpoint>幾個屬性address(地址) binding(綁定),Contract(契約),這幾個屬性正是上面所說的"ABC" 注意一下 binding里填的是BasicHttpBinding、WsHttpBinding、NetTcpBinding這些值,而確切使用哪一個binding呢,就需要在bindingConfiguration中設置,值是使用的<binding>的name值。contract項目中contract的契約接口的完全限定名,這里關於binding的配置接下來會介紹。address沒填值,這里在<host>中已經給定了一個地址了。
介紹完<endpoint>,再看看<endpoint>外面的。<endpoint>包含在<services>的<service>下,這里的<serivces>是一個集合,里面可以包含多個服務,每個服務都會有特定的命名(name),而name則是項目里頭實現契約(Contract)的服務(Service)的類的完全限定名。這里對servicebehavior進行了一些設置,具體的內容在名為te的<servicebehavior>中。
既然上面有配置有涉及到binding和behavior,下面則分別對兩者進行配置。
<bindings> <ws2007HttpBinding> <binding name="transportWS2007HttpBinding" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647"> <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/> <security mode="Message"> <transport clientCredentialType="None"/> </security> </binding> </ws2007HttpBinding> <basicHttpBinding> <binding name="newBinding" maxBufferPoolSize="21474835647" maxReceivedMessageSize="2147483647" messageEncoding="Text"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> </binding> </basicHttpBinding> </bindings>
bindings這部分和services一樣,也是一個集合,里面包含着各種類型的binding,例如在<ws2007HttpBinding>里面的<binding>才是確切的某一個binding, <endpoint>使用時,bindingConfiguration的名稱要寫對外,binding的類型也不能錯。<binding>里面的子節點和屬性就不再一一介紹了,若是要通過WCF傳輸比較大的數據時,要在binding的屬性和<readerQuotas>設置一下。
<behaviors> <serviceBehaviors> <behavior name="te"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors>
最后到behaviors了。同理,behaviors也是一個集合,里面有兩種類型,一種是serviceBehaviors,用於配置service的;另一種是endpointBehaviors,用於配置endpoint的。這兩種類型都是一個集合,子節點<behavior>是它們的子項,以name來區分各個behavior,至於里面有什么屬性和子項也不多說了,使用時在相應的service或endpoint的behaviorConfiguration屬性填上behavior的name值就行了。
服務端的配置就嘮叨到這里,下面到客戶端的。
<client> <endpoint address="http://localhost:9091/logicService" binding="ws2007HttpBinding" bindingConfiguration="WS2007HttpBinding_ILogic" contract="Proxy.ILogic" name="WS2007HttpBinding_ILogic"> </endpoint> </client>
首先也是是終結點,客戶端的終結點放在client里,里面也是有"ABC",這里的address一定要與服務端配置的一樣,否則找不到相應的服務的。binding的類型也要與服務端的一樣,contract則是用svcutil或其他工具生成的代碼里的那個類的完全限定名。
<ws2007HttpBinding> <binding name="WS2007HttpBinding_ILogic" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="1024" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> </binding> </ws2007HttpBinding>
另一個還要提的是這個binding,客戶端的binding比服務端的要配置多一點東西closeTimeout,openTimeout,receiveTimeout 大致與服務端一樣。
另外若要傳輸比較的大數據時,可以按我這樣來配,其實這個配置已經適用於傳輸幾M的圖片。由於是個入門者,很多東西的理解還不夠透徹,以上有說錯的還請各位批評指出。謝謝!