WCF 服務會公開許多會影響效能的組態參數。 本主題提供設定這些組態參數最佳值的一般指引,以改善 WCF 服務的效能。
實作後端 WCF 服務的 ServiceThrottling 行為限制
在後端 WCF 服務中實現 serviceThrottling 行為。 服務節流可讓您平衡後端 WCF 伺服器上的負載,並確保資源配置。 後端 WCF 服務的 serviceThrottling 行為是修改 WCF 服務組態檔中 maxConcurrentCalls、 maxConcurrentSessions 和 maxConcurrentInstances 參數的值來設定。 將 maxConcurrentCalls、 maxConcurrentSessions 和 maxConcurrentInstances 設定為大於 16 * CPU 或 CPU 核心數目的值。 例如,在具有 8 個 CPU 核心的計算機上,將 maxConcurrentCalls、 maxConcurrentSessions 和 maxConcurrentInstances 設定為大於 128 (16 * 8 = 128) 的值,如下所示:
<serviceThrottling
maxConcurrentCalls="200"
maxConcurrentSessions="200"
maxConcurrentInstances="200" />
在後端 WCF 服務 web.config 檔案中,增加 NetTcpBinding.ListenBacklog 和 NetTcpBinding.MaxConnections 屬性的預設值
NetTcpBinding.ListenBacklog 屬性可控制 Web 服務可能擱置的佇列連線要求數目上限。 NetTcpBinding.MaxConnections 屬性會控制在用戶端上後續重複使用的連線數目上限,以及伺服器上允許擱置分派的連線數目上限。 每個屬性都會使用預設值 10,這個值可能並不理想,特別是在需要高效能的文件處理過程情境中。
請考慮針對使用實作 netTcpBinding 系結類別的 WCF 服務,為高輸送量的文件處理案例增加這些屬性的預設值。
在下列範例中, listenBacklog 和 maxConnections 參數都會設定為 “200”。
<netTcpBinding>
<binding name="netTcpBinding"
closeTimeout="00:10:00"
openTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
transactionFlow="false"
transferMode="Buffered"
transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard"
listenBacklog="200"
maxBufferPoolSize="1048576"
maxBufferSize="10485760"
maxConnections="200"
maxReceivedMessageSize="10485760">
<readerQuotas
maxDepth="32"
maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<reliableSession
ordered="true"
inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
如需 NetTcpBinding.ListenBacklog 屬性的詳細資訊,請參閱 NetTcpBinding.ListenBacklog 屬性。
如需 NetTcpBinding.MaxConnections 屬性的詳細資訊,請參閱 NetTcpBinding.MaxConnections 屬性。
移除不需要執行 WCF Web 服務的 ASP.NET HTTP 模組
根據預設,在 IIS 6.0 的要求管線和 IIS 7.5/7.0 的傳統或整合式管線中,會定義數個 ASP.NET HTTPModules。 這些元件會攔截並處理所有傳入要求。 默認模組定義於 32 位 ASP.NET 應用程式之 %windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG 資料夾中的 web.config 檔案中,以及在 64 位 ASP.NET 應用程式的 %windir%\Microsoft.NET\Framework64\v2.0.50727\CONFIG 資料夾中的 web.config 檔案中,如下列代碼段所示。
<httpModules>
<add name="OutputCache" type="System.Web.Caching.OutputCacheModule"/>
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule"/>
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"/>
<add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule"/>
<add name="RoleManager" type="System.Web.Security.RoleManagerModule"/>
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"/>
<add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule"/>
<add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule"/>
<add name="Profile" type="System.Web.Profile.ProfileModule"/>
<add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</httpModules>
在大部分情況下,不需要載入所有這些模組。 因此,在執行 WCF Web 服務時,可以排除下列 HTTPModules 來改善效能:
會期
WindowsAuthentication
FormsAuthentication
護照認證
角色管理器 (RoleManager)
匿名識別
個人檔案
如需使用異步 WCF HTTP 模組/處理程式來改善 IIS 7.5/7.0 裝載 WCF 服務延展性的詳細資訊,請參閱文龍董的部落格 Orcas SP1 改進:IIS7 的異步 WCF HTTP 模組/處理程式,以提升伺服器延展性。