学习 WCF (4)--学会使用配置文件
無論是Web應(yīng)用程序還是Win應(yīng)用程序,我們都會經(jīng)常用到配置文件。WCF作為分布式開發(fā)的基礎(chǔ)框架,在定義服務(wù)以及定義消費服務(wù)的客戶端時,都使用了配置文件的方法。配置文件的重要性和實用性是大家所熟知的,它可以給我們WCF開發(fā)的靈活性上帶來很大的提高。下面說說我學(xué)習(xí)使用配置文件的所得。
?? WCF的配置使用.NET Framework的System.Configuration配置系統(tǒng)。在Visual Studio中配置一個WCF服務(wù)時,如果是自托管宿主或Windows Services宿主,則配置文件為App.confing,如果是IIS宿主,則配置文件為Web.config。
?? 先來看看簡單的system.ServiceModel結(jié)構(gòu)
Code<system.ServiceModel><services><service><endpoint/></service></services><bindings><!—定義一個或多個系統(tǒng)提供的binding元素,例如<basicHttpBinding>?-->?<!—也可以是自定義的binding元素,如<customBinding>.?--><binding><!—例如<BasicHttpBinding>元素.?--></binding></bindings><behaviors><!—一個或多個系統(tǒng)提供的behavior元素.?--><behavior><!—例如<throttling>元素.?--></behavior></behaviors></system.ServiceModel>(1)<services>
服務(wù)是在配置文件的 services 節(jié)中定義的。每個服務(wù)都有自己的 service 配置節(jié)。在Service中定義特定服務(wù)的address,binding,contract(也就是傳說中重要的ABC)。
Code????????<services><service?name="WCFStudent.WCFStudentText"?behaviorConfiguration="ServiceBehavior"><endpoint?address=""?binding="wsHttpBinding"?contract="WCFStudent.IStuServiceContract"></endpoint><endpoint?address="mex"?binding="mexHttpBinding"?contract="IMetadataExchange"/></service></services><!--一個Service中可以有多個endpoint,這樣就可以同時定制多個服務(wù)。如果address值為空,那么endpoint的地址就是默認(rèn)的基地址(Base?Address)。那么則需要在host中聲明Base?Address-->(2)<bindings>
此節(jié)包含標(biāo)準(zhǔn)綁定和自定義綁定的集合。每一項都是一個可由其唯一 name 進行標(biāo)識的 binding 元素。服務(wù)通過用 name 與綁定進行鏈接來使用綁定。
個人感覺比較常用的是<basicHttpBinding>,<basicHttpBinding>等。由于屬性比較多,在這里就不一一說明了。
Code<bindings><basicHttpBinding><binding?name="IStuServiceContract"?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="65536"
????????????????????messageEncoding="Text"?textEncoding="utf-8"?useDefaultWebProxy="true"
????????????????????allowCookies="false"></basicHttpBinding></bindings><!--相應(yīng)屬性的設(shè)置大家可以參看MSDN.
http://msdn.microsoft.com/zh-cn/library/ms731361.aspx-->
(3)<behaviors>
此元素定義名為 endpointBehaviors 和 serviceBehaviors 的兩個子集合。每個集合分別定義終結(jié)點和服務(wù)所使用的行為元素。每個行為元素由其唯一的 name 屬性標(biāo)識。如果需要指定服務(wù)在執(zhí)行方面的相關(guān)特性時,就必須定義服務(wù)的behavior。在WCF中,定義behavior就可以設(shè)置服務(wù)的運行時屬性,甚至于通過自定義behavior插入一些自定義類型。
Code<behaviors><serviceBehaviors><behavior?name="ServiceBehavior"><!--?為避免泄漏元數(shù)據(jù)信息,請在部署前將以下值設(shè)置為?false?并刪除上面的元數(shù)據(jù)終結(jié)點??--><serviceMetadata?httpGetEnabled="true"/><!--?要接收故障異常詳細(xì)信息以進行調(diào)試,請將以下值設(shè)置為?true。在部署前設(shè)置為?false?以避免泄漏異常信息--><serviceDebug?includeExceptionDetailInFaults="false"/></behavior></serviceBehaviors></behaviors>(4)<client>
?有的時候我們還需要定義<client>節(jié)點,主要定義客戶端可以連接的終結(jié)點的列表。
Code<client><endpoint?address="http://localhost:39113/WCFServiceText/WCFStudentText.svc"????????????????binding="wsHttpBinding"?bindingConfiguration="WSHttpBinding_IStuServiceContract"
????????????????contract="ServiceReference1.IStuServiceContract"?name="WSHttpBinding_IStuServiceContract"><identity><dns?value="localhost"?/></identity></endpoint></client><!--如果宿主是IIS,WIN應(yīng)用程序調(diào)用WCF時,會自動在App.config中生成<client>節(jié)的設(shè)置-->
三種宿主形式的配置文件的聲明基本上一樣。如果使用svcutil.exe生成客戶程序,會在svcutil.exe的根目錄中生成一個配置文件,值得大家一看:
Code<?xml?version="1.0"?encoding="utf-8"?><configuration><system.serviceModel><bindings><wsHttpBinding><binding?name="WSHttpBinding_IStuServiceContract"?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="65536"
????????????????????messageEncoding="Text"?textEncoding="utf-8"?useDefaultWebProxy="true"
????????????????????allowCookies="false"><readerQuotas?maxDepth="32"?maxStringContentLength="8192"?maxArrayLength="16384"
????????????????????????maxBytesPerRead="4096"?maxNameTableCharCount="16384"?/><reliableSession?ordered="true"?inactivityTimeout="00:10:00"
????????????????????????enabled="false"?/><security?mode="Message"><transport?clientCredentialType="Windows"?proxyCredentialType="None"
????????????????????????????realm=""?/><message?clientCredentialType="Windows"?negotiateServiceCredential="true"
????????????????????????????algorithmSuite="Default"?establishSecurityContext="true"?/></security></binding></wsHttpBinding></bindings><client><endpoint?address="http://localhost:39113/WCFServiceText/WCFStudentText.svc"
????????????????binding="wsHttpBinding"?bindingConfiguration="WSHttpBinding_IStuServiceContract"
????????????????contract="IStuServiceContract"?name="WSHttpBinding_IStuServiceContract"><identity><dns?value="localhost"?/></identity></endpoint></client></system.serviceModel></configuration>
??? 文章作者:高維鵬(Brian)
??? 文章出處:http://www.cnblogs.com/gaoweipeng
??? 歡迎轉(zhuǎn)載,轉(zhuǎn)載時請注明出處。謝謝合作。
總結(jié)
以上是生活随笔為你收集整理的学习 WCF (4)--学会使用配置文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查理和巧克力工厂国语版下载(查理和巧克力
- 下一篇: 学习 WCF (5)--学习Bindin