標籤:
一、WCF中的ABC
情境:公司讓你送一份合約檔案,送檔案的過程你可以選擇的交通方式有打的,地鐵或公交。
到了對方公司後,你要找到某負責人,並且要一份收到合約檔案的回執和相應檔案
要完成這項工作任務主要以下幾個步驟
1.知道對方公司的地址
即WCF中的A,通過Address我們可以找到我們要調用的WCF服務
2.選擇交通方式
即WCF中的B,通過Binding來實現Client和Service通訊的所有底層細節。
傳輸採用什麼編碼,傳輸使用什麼協議,等等
3.到了對方公司我們能做哪些事
即WCF中的C,通過Contract我們知道了哪些事我們能做,那些事我們不能做
二、Endpoint(終結點)
WCF實現了網路系統的各個應用程式的通訊。各個應用程式的通訊是以”終結點”來實現的;
上面例子中的ABC即是Endpoint的組成部分,它是伺服器通訊調用的入口;
只有當Client與Service的終結點完全符合的時候才能進行通訊
三、程式碼分析
上一篇中在用戶端和服務端都產生了兩個設定檔
用戶端配置
<?xml version="1.0" encoding="utf-8"?><!-- 有關如何配置 ASP.NET 應用程式的詳細資料,請訪問 http://go.microsoft.com/fwlink/?LinkId=169433 --><configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IUser" /> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:9003/User.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUser" contract="WCFService.IUser" name="BasicHttpBinding_IUser" /> </client> </system.serviceModel></configuration>
伺服器配置
<?xml version="1.0" encoding="utf-8"?><configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5"/> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- 為避免泄漏中繼資料資訊,請在部署前將以下值設定為 false --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- 要接收故障異常詳細資料以進行調試,請將以下值設定為 true。在部署前設定為 false 以避免泄漏異常資訊 --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <!-- 若要在調試過程中瀏覽 Web 應用程式根目錄,請將下面的值設定為 True。 在部署之前將該值設定為 False 可避免泄露 Web 應用程式檔案夾資訊。 --> <directoryBrowse enabled="true"/> </system.webServer></configuration>
我們在服務端的system.serviceMode節點中並沒有看到上面說的Endpoint是因為我們WCF寄宿在IIS上,而IIS預設監聽的就是http協議(B確定了),而地址也是相對於IIS上檔案的地址(A確定了),合約更不用說了,找到User.svc上面都有了(C確定了)。所以服務端就沒有顯示Endpint配置也能接收到用戶端的資訊
我們將服務端的設定檔改成如下檔案,程式照樣可以運行
<?xml version="1.0" encoding="utf-8"?><configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5"/> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- 為避免泄漏中繼資料資訊,請在部署前將以下值設定為 false --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- 要接收故障異常詳細資料以進行調試,請將以下值設定為 true。在部署前設定為 false 以避免泄漏異常資訊 --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <!--這是新加的代碼--> <client> <endpoint address="http://localhost:9003/User.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUser" contract="WCFService.IUser" name="BasicHttpBinding_IUser" /> </client> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <!-- 若要在調試過程中瀏覽 Web 應用程式根目錄,請將下面的值設定為 True。 在部署之前將該值設定為 False 可避免泄露 Web 應用程式檔案夾資訊。 --> <directoryBrowse enabled="true"/> </system.webServer></configuration>
WCF學習筆記(2)-WCF的通訊過程