Skip to main content
 首页 » 编程设计

c#之提高 WCF 服务性能

2024年02月23日26linjiqin

我的公司拥有一台 24 核和大量 RAM 的服务器。操作系统是Windows 2008 R2 SP1。我在此服务器上部署了一个托管在 IIS 中的 WCF 应用程序。在同一台服务器上,我安装了一个多线程客户端,它会尽可能频繁地调用 WCF 服务之一。我遇到了一个瓶颈:服务器上的所有内核都被使用了,但是处于很低的水平,所以CPU消耗不会超过10%。我的 WCF 服务配置如下:

  <system.serviceModel> 
<services> 
  <service behaviorConfiguration="myBehavior" name="..."> 
    <endpoint address="" binding="netTcpBinding" bindingConfiguration="myBinding" bindingNamespace="..." contract="..."/> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
  </service> 
</services> 
 
<behaviors> 
  <serviceBehaviors> 
    <behavior name="myBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="200" /> 
    </behavior> 
  </serviceBehaviors> 
</behaviors> 
 
<bindings> 
  <netTcpBinding> 
    <binding name="myBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="2147483647" maxReceivedMessageSize="2147483647" portSharingEnabled="false" transactionFlow="false" listenBacklog="2147483647"> 
      <security mode="None"> 
        <message clientCredentialType="None"/> 
        <transport protectionLevel="None" clientCredentialType="None"/> 
      </security> 
      <reliableSession enabled="false"/> 
      <readerQuotas maxDepth="64" maxStringContentLength="204800" maxArrayLength="204800" maxBytesPerRead="4096" maxNameTableCharCount="16384"/> 
    </binding> 
  </netTcpBinding> 
</bindings> 

我的服务还具有以下属性:InstanceContextMode.Single 和 ConcurrencyMode.Multiple。有没有人遇到过类似的问题?我搜索了几天的解决方案,但我还没有找到:(

提前致谢!

请您参考如下方法:

我不确定您是如何衡量性能瓶颈的,但无论您生成多少客户端调用,该服务一次只能处理 200 个调用。 netTcpBinding 使用 session ,因此存在 maxConcurrentSessions="200" 设置的限制。您已将服务配置为多线程单例,通过 maxConcurrentCalls="200" 设置限制为 200 个并发调用。 CPU 负载还取决于每次调用期间执行的工作量以及它是否受 IO 限制。查看 serviceThrottling element文档并尝试增加这两个设置以查看您的吞吐量是否有所提高。

如果您的服务实现允许,我建议您尝试使用 InstanceContextMode.PerSession 和 ConcurrencyMode.Single 配置服务,以将吞吐量与当前配置进行比较。 IIS 7x 使用 Windows Process Activation Service (WAS) 托管 WCF 服务。 WAS 旨在处理并发性。单例配置否定了使用 WAS 的值(value)。