Question

How do I get and set the number of threads allowed in a WCF server thread pool to handle incoming requests in parallel?

I am using a WebHttpBinding.

Was it helpful?

Solution

You get them via the ServiceThrottlingBehavior class, The defaults are as follows

.NET 3.0/3.5

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

.NET 4.0+

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

The thing you are most interested in is MaxConcurrentInstances as that is what would consume a thread. Normally if you where to configure this you would set it up in your app.config file

<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>

Looking up via code is much more difficult to do (I could not figure out how, I normally just use configuration files)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top