Dot-Net

WCF wsHttpBinding 與 http keepalive

  • July 6, 2011

我有一個使用 wsHttpBinding 的 WCF 客戶端,我想啟用 http keep-alive。

我希望我可以通過更改客戶端配置來打開它……我找到了很多關於如何為 basicHttp 綁定打開保持活動的描述,但是 wsHttpBinding 沒有運氣……這可能嗎?

非常感謝。

這是我的客戶端綁定:

 <wsHttpBinding>
   <binding name="WSHttpBinding_IRepositoryService" closeTimeout="00:00:10"
     openTimeout="00:00:10" receiveTimeout="00:05:00" sendTimeout="00:05:00"
     bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
     maxBufferPoolSize="524288" maxReceivedMessageSize="655360" messageEncoding="Mtom"
     textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
     <readerQuotas maxDepth="32" maxStringContentLength="81920" maxArrayLength="163840"
       maxBytesPerRead="409600" maxNameTableCharCount="16384" />
     <reliableSession ordered="true" inactivityTimeout="00:10:00"
       enabled="true" />
     <security mode="Message">
       <transport clientCredentialType="Windows" proxyCredentialType="None"
         realm="">
         <extendedProtectionPolicy policyEnforcement="Never" />
       </transport>
       <message clientCredentialType="Windows" negotiateServiceCredential="true"
         algorithmSuite="Default" establishSecurityContext="true" />
     </security>

   </binding>
</wsHttpBinding>

您必須使用自定義綁定才能禁用Keep-Alive標頭,因為該功能未在任何內置綁定類中公開。

無需從頭開始定義自定義綁定即可實現此目的的最簡單方法是在程式碼中自定義與客戶端代理關聯的現有BasicHttpBindingWSHttpBinding實例。

這是一個例子:

var proxy = new MyServiceClient();
var customBinding = new CustomBinding(proxy.Endpoint.Binding);
var transportElement = customBinding.Elements.Find<HttpTransportBindingElement>();
transportElement.KeepAliveEnabled = false;

proxy.Endpoint.Binding = customBinding;

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