Dot-Net

設置 WCF 客戶端和伺服器時,配置文件必須同步到什麼程度?

  • February 25, 2015

大多數 WCF 範例向您展示瞭如何配置 WCF 客戶端和伺服器。現在,如果它們之間的配置略有不同,會發生什麼?我的意思是,誰說了算?

讓我們以這個客戶端配置為例:

<configuration>
<system.serviceModel>
   <bindings>
       <wsHttpBinding>
           <binding name="WSHttpBinding_ISampleService" closeTimeout="00:01:00"
               openTimeout="00:01:00" receiveTimeout="00:01: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="None" proxyCredentialType="None"
                       realm="" />
                   <message clientCredentialType="Windows" negotiateServiceCredential="true"
                       algorithmSuite="Default" establishSecurityContext="true" />
               </security>
           </binding>
       </wsHttpBinding>
   </bindings>
   <client>
       <endpoint address="http://localhost:8080/SampleService" binding="wsHttpBinding"
           bindingConfiguration="WSHttpBinding_ISampleService" contract="ISampleService"
           name="WSHttpBinding_ISampleService">
       </endpoint>
   </client>
</system.serviceModel>

通常,伺服器端將在其暴露的伺服器上配置完全相同的綁定。

但是,如果在伺服器端使用 openTimeout = 00:00:30 定義,現在會發生什麼。超時時間是多少?誰贏?我對所有其他參數做同樣的問題。

整個事情看起來一團糟,你怎麼知道配置的每個元素(綁定、客戶端、服務、行為等)及其所有細節,需要哪些參數以及在哪一側(客戶端或伺服器)?

似乎您可以在伺服器端使用所有超時參數定義整個綁定,而在客戶端只需放置所需的最低配置,以便接受來自伺服器的所有參數。但是現在,考慮到伺服器具有更深入的配置,客戶端上所需的最低參數是多少?

使用 WCF 配置客戶端和伺服器時,關於配置的每個元素的參數的最佳實踐是什麼:綁定、服務、客戶端/端點和行為?

當客戶端和伺服器之間定義衝突的參數時,WCF 如何處理它?

為了在您對我之前的回答的最後評論中解決您的請求,我試圖提出我將如何為任何給定服務創建(和修改)伺服器端和客戶端配置的方法。這是基於我讀過的理論(書籍、部落格)、我在 Juval Lowy’s 中學到的東西WCF Master Class,以及在幾個大型服務實施項目中的大量實踐經驗——這在網路上的一個地方是不可用的或在一本書中……所以它是這樣的:

我基本上會從頭開始。首先考慮您的服務:

  • 您的服務位於哪個地址?
  • 你想支持什麼綁定?

最簡單的場景:單服務,單端點,basicHttpBinding,全部預設

服務配置:

<system.serviceModel>
  <services>
     <service name="YourNamespace.YourServiceClass">
        <endpoint name="Default"
            address="http://YourServer/SomeVirtualDirectory/YourService.svc"
            binding="basicHttpBinding"
            contract="YourNamespace.IYourServiceContract" />
     </service>
  </services>
</system.serviceModel>

對應的客戶端配置:

<system.serviceModel>
  <client name="Default">
     <endpoint name="Default"
         address="http://YourServer/SomeVirtualDirectory/YourService.svc"
         binding="basicHttpBinding"
         contract="YourClientProxyNamespace.IYourServiceContract" />
  </client>
</system.serviceModel>

**那麼只有在你真的必須的時候才改變一些東西!**最重要的是:永遠不要讓 Visual Studio(添加服務參考)或svcutil.exe搞砸你的配置!像保護你的掌上明珠一樣保護它!

然後:例如,如果您的數據傳輸花費的時間超過了預設超時時間 1 分鐘所允許的時間,請在服務端和客戶端更改該單個設置。通過定義自定義綁定配置並從您的端點引用它來做到這一點 -**但只改變它 - 沒有更多!**保留其他所有內容,使用預設值。除非你絕對必須改變任何東西(並且知道你在做什麼,以及你為什麼這樣做),否則永遠不要改變任何東西。

請注意:sendTimeout客戶端上的(在整個消息發送之前允許的時間)將對應於receiveTimeout伺服器上的 - 整個消息進入的時間(有關更多資訊,請參閱這篇優秀的部落格文章和這個MSDN 論壇主題)

服務配置:

<system.serviceModel>
   <bindings>
     <basicHttpBinding>
       <binding name="ExtendedTimeout"
                receiveTimeout="00:05:00" />
     </basicHttpBinding>
   </bindings>
   <services>
     <service name="YourNamespace.YourServiceClass">
       <endpoint name="Default"
           address="http://YourServer/SomeVirtualDirectory/YourService.svc"
           binding="basicHttpBinding"
           bindingConfiguration="ExtendedTimeout"
           contract="YourNamespace.IYourServiceContract" />
     </service>
   </services>
 </system.serviceModel>

對應的客戶端配置:

<system.serviceModel>
  <bindings>
     <basicHttpBinding>
        <binding name="ExtendedTimeout"
                 sendTimeout="00:05:00" />
     </basicHttpBinding>
  </bindings>
  <client name="Default">
     <endpoint name="Default"
         address="http://YourServer/SomeVirtualDirectory/YourService.svc"
         binding="basicHttpBinding"
         bindingConfiguration="ExtendedTimeout"
         contract="YourClientProxyNamespace.IYourServiceContract" />
  </client>
</system.serviceModel>

當您需要其他更改時,例如服務端的多個端點,或本地設置,例如bypassProxyOnLocal- 調整您的配置,仔細地,逐步,手動,並將您的配置視為整個服務的極其重要的部分 - 照顧好它,把它放在版本控制等中。

引用自:https://stackoverflow.com/questions/4879310