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 httpModules
默认情况下,在 IIS 6.0 的请求管道和 IIS 7.5/7.0 的经典管道或集成管道中定义多个 ASP.NET httpModule。 这些组件截获并处理所有传入的请求。 默认模块 web.config 在 32 位 ASP.NET 应用程序的 %windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG 文件夹中以及 %windir%\Microsoft.NET\Framework64\v2.0.50727\CONFIG 文件夹中为 64 位 ASP.NET 应用程序定义,如以下代码片段所示。
<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 服务时消除以下 httpModule,可以提高性能:
会话
WindowsAuthentication
FormsAuthentication
护照认证
角色管理器
匿名身份识别
个人资料
有关使用异步 WCF HTTP 模块/处理程序改进 IIS 7.5/7.0 托管 WCF 服务的可伸缩性的详细信息,请参阅文龙董的博客 Orcas SP1 改进:IIS7 的异步 WCF HTTP 模块/处理程序,以提高服务器可伸缩性。