如何获取/设置WCF用于并行处理请求的线程数?

时间:2014-02-18 22:53:34

标签: c# .net multithreading wcf threadpool

如何获取和设置WCF服务器线程池中允许并行处理传入请求的线程数?

我正在使用WebHttpBinding。

1 个答案:

答案 0 :(得分:3)

您可以通过ServiceThrottlingBehavior类获取它们,默认值如下

.NET 3.0 / 3.5

  • MaxConcurrentSessions:10
  • MaxConcurrentCalls:16
  • MaxConcurrentInstances:26

.NET 4.0 +

  • MaxConcurrentSessions:100 * ProcessorCount
  • MaxConcurrentCalls:16 * ProcessorCount
  • MaxConcurrentInstances:MaxConcurrentSessions + MaxConcurrentCalls

你最感兴趣的是MaxConcurrentInstances,因为这会消耗一个线程。通常,如果您在何处配置它,则应在app.config文件中进行设置

<configuration>
  <appSettings>
    <!-- use appSetting to configure base address provided by host -->
    <add key="baseAddress" value="http://localhost:8080/ServiceMetadata" />
  </appSettings>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="Throttled" >
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/SampleService"/>
          </baseAddresses>
        </host>
        <endpoint
          address=""
          binding="wsHttpBinding"
          contract="Microsoft.WCF.Documentation.ISampleService"
         />
        <endpoint
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange"
         />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior  name="Throttled">
          <serviceThrottling 
            maxConcurrentCalls="1" 
            maxConcurrentSessions="1" 
            maxConcurrentInstances="1"
          />
          <serviceMetadata 
            httpGetEnabled="true" 
            httpGetUrl=""
          />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

通过代码查找起来要困难得多(我无法弄明白,我通常只使用配置文件)