WCF配置心得
根據(jù)蔣金楠老師的博文所說的, WCF的終結(jié)點(diǎn)有三個要素組成,分別是地址(Address)、綁定(Binding)和契約(Contract),簡記可寫成Endpoint = ABC。
地址:地址決定了服務(wù)的位置,解決了服務(wù)尋址的問題。
綁定:綁定實(shí)現(xiàn)了通信的所有細(xì)節(jié),包括網(wǎng)絡(luò)傳輸、消息編碼,以及其他為實(shí)現(xiàn)某種功能對消息進(jìn)行的相應(yīng)處理。綁定的類型包括BasicHttpBinding、WsHttpBinding、NetTcpBinding等。
契約:契約是對服務(wù)操作的抽象,也是對消息交換模式以及消息結(jié)構(gòu)的定義。
以上這些內(nèi)容摘抄自蔣老師的博文。理解的這些對配置WCF很有幫助。
那下面就一步步來配置一個WCF。
首先是服務(wù)端,
一個WCF的核心是終結(jié)點(diǎn),那么先把終結(jié)點(diǎn)寫列出來,
<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中設(shè)置,值是使用的<binding>的name值。contract項(xiàng)目中contract的契約接口的完全限定名,這里關(guān)于binding的配置接下來會介紹。address沒填值,這里在<host>中已經(jīng)給定了一個地址了。
介紹完<endpoint>,再看看<endpoint>外面的。<endpoint>包含在<services>的<service>下,這里的<serivces>是一個集合,里面可以包含多個服務(wù),每個服務(wù)都會有特定的命名(name),而name則是項(xiàng)目里頭實(shí)現(xiàn)契約(Contract)的服務(wù)(Service)的類的完全限定名。這里對servicebehavior進(jìn)行了一些設(shè)置,具體的內(nèi)容在名為te的<servicebehavior>中。
既然上面有配置有涉及到binding和behavior,下面則分別對兩者進(jìn)行配置。
<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>里面的子節(jié)點(diǎn)和屬性就不再一一介紹了,若是要通過WCF傳輸比較大的數(shù)據(jù)時,要在binding的屬性和<readerQuotas>設(shè)置一下。
<behaviors>
<serviceBehaviors>
<behavior name="te">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
最后到behaviors了。同理,behaviors也是一個集合,里面有兩種類型,一種是serviceBehaviors,用于配置service的;另一種是endpointBehaviors,用于配置endpoint的。這兩種類型都是一個集合,子節(jié)點(diǎn)<behavior>是它們的子項(xiàng),以name來區(qū)分各個behavior,至于里面有什么屬性和子項(xiàng)也不多說了,使用時在相應(yīng)的service或endpoint的behaviorConfiguration屬性填上behavior的name值就行了。
服務(wù)端的配置就嘮叨到這里,下面到客戶端的。
<client>
<endpoint address="http://localhost:9091/logicService" binding="ws2007HttpBinding"
bindingConfiguration="WS2007HttpBinding_ILogic" contract="Proxy.ILogic"
name="WS2007HttpBinding_ILogic">
</endpoint>
</client>
首先也是是終結(jié)點(diǎn),客戶端的終結(jié)點(diǎn)放在client里,里面也是有"ABC",這里的address一定要與服務(wù)端配置的一樣,否則找不到相應(yīng)的服務(wù)的。binding的類型也要與服務(wù)端的一樣,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比服務(wù)端的要配置多一點(diǎn)東西closeTimeout,openTimeout,receiveTimeout 大致與服務(wù)端一樣。
另外若要傳輸比較的大數(shù)據(jù)時,可以按我這樣來配,其實(shí)這個配置已經(jīng)適用于傳輸幾M的圖片。由于是個入門者,很多東西的理解還不夠透徹,以上有說錯的還請各位批評指出。謝謝!
相關(guān)文章
Cordova插件實(shí)現(xiàn)JavaScript與Java的通信的詳細(xì)過程
本文將結(jié)合最常用的華為推送服務(wù)Cordova插件,介紹HMS Core用到的JS-Java消息交互方式,講解在JS側(cè)如何調(diào)用Java側(cè)接口,最終實(shí)現(xiàn)HMS Core能力,感興趣的朋友一起學(xué)習(xí)下吧2021-06-06
計算機(jī)網(wǎng)絡(luò)編程MQTT協(xié)議基礎(chǔ)原理詳解
這篇文章主要為大家介紹了計算機(jī)編程MQTT協(xié)議的基礎(chǔ)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11
git merge --ff/--no-ff/--ff-only 三種選項(xiàng)參數(shù)的區(qū)別解析
這篇文章主要介紹了git merge --ff/--no-ff/--ff-only 三種選項(xiàng)參數(shù)的區(qū)別解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04

