学习 WCF (4)--学会使用配置文件
無論是Web應用程序還是Win應用程序,我們都會經常用到配置文件。WCF作為分布式開發的基礎框架,在定義服務以及定義消費服務的客戶端時,都使用了配置文件的方法。配置文件的重要性和實用性是大家所熟知的,它可以給我們WCF開發的靈活性上帶來很大的提高。下面說說我學習使用配置文件的所得。
?? WCF的配置使用.NET Framework的System.Configuration配置系統。在Visual Studio中配置一個WCF服務時,如果是自托管宿主或Windows Services宿主,則配置文件為App.confing,如果是IIS宿主,則配置文件為Web.config。
?? 先來看看簡單的system.ServiceModel結構
Code<system.ServiceModel><services><service><endpoint/></service></services><bindings><!—定義一個或多個系統提供的binding元素,例如<basicHttpBinding>?-->?<!—也可以是自定義的binding元素,如<customBinding>.?--><binding><!—例如<BasicHttpBinding>元素.?--></binding></bindings><behaviors><!—一個或多個系統提供的behavior元素.?--><behavior><!—例如<throttling>元素.?--></behavior></behaviors></system.ServiceModel>(1)<services>
服務是在配置文件的 services 節中定義的。每個服務都有自己的 service 配置節。在Service中定義特定服務的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,這樣就可以同時定制多個服務。如果address值為空,那么endpoint的地址就是默認的基地址(Base?Address)。那么則需要在host中聲明Base?Address-->(2)<bindings>
此節包含標準綁定和自定義綁定的集合。每一項都是一個可由其唯一 name 進行標識的 binding 元素。服務通過用 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><!--相應屬性的設置大家可以參看MSDN.
http://msdn.microsoft.com/zh-cn/library/ms731361.aspx-->
(3)<behaviors>
此元素定義名為 endpointBehaviors 和 serviceBehaviors 的兩個子集合。每個集合分別定義終結點和服務所使用的行為元素。每個行為元素由其唯一的 name 屬性標識。如果需要指定服務在執行方面的相關特性時,就必須定義服務的behavior。在WCF中,定義behavior就可以設置服務的運行時屬性,甚至于通過自定義behavior插入一些自定義類型。
Code<behaviors><serviceBehaviors><behavior?name="ServiceBehavior"><!--?為避免泄漏元數據信息,請在部署前將以下值設置為?false?并刪除上面的元數據終結點??--><serviceMetadata?httpGetEnabled="true"/><!--?要接收故障異常詳細信息以進行調試,請將以下值設置為?true。在部署前設置為?false?以避免泄漏異常信息--><serviceDebug?includeExceptionDetailInFaults="false"/></behavior></serviceBehaviors></behaviors>(4)<client>
?有的時候我們還需要定義<client>節點,主要定義客戶端可以連接的終結點的列表。
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應用程序調用WCF時,會自動在App.config中生成<client>節的設置-->
三種宿主形式的配置文件的聲明基本上一樣。如果使用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
??? 歡迎轉載,轉載時請注明出處。謝謝合作。
總結
以上是生活随笔為你收集整理的学习 WCF (4)--学会使用配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查理和巧克力工厂国语版下载(查理和巧克力
- 下一篇: 学习 WCF (5)--学习Bindin