Condividi tramite


Using ServiceThrottlingBehavior to Control WCF Service Performance

The ServiceThrottlingBehavior class exposes properties that you can use to limit how many instances or sessions are created at the application level. Using this behavior, you can fine-tune the performance of your Windows Communication Foundation (WCF) application.

Controlling Service Instances and Concurrent Calls

Use the MaxConcurrentCalls property to specify the maximum number of messages actively processing across a ServiceHost class, and the MaxConcurrentInstances property to specify the maximum number of InstanceContext objects in the service.

Because determining the settings for these properties usually takes place after real-world experience running the application against loads, the settings for the ServiceThrottlingBehavior properties is typically specified in an application configuration file using the <serviceThrottling> element.

The following code example shows the use of the ServiceThrottlingBehavior class from an application configuration file that sets the MaxConcurrentSessions, MaxConcurrentCalls, and MaxConcurrentInstances properties to 1 as a trivial example. Real-world experience determines the optimal settings for any particular application.

<configuration>
  <appSettings>
    <!-- use appSetting to configure base address provided by host -->
    <add key="baseAddress" value="https://localhost:8080/ServiceMetadata" />
  </appSettings>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="Throttled" >
        <host>
          <baseAddresses>
            <add baseAddress="https://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>

The exact run-time behavior depends upon the values of the ConcurrencyMode and InstanceContextMode properties, which control how many messages can execute inside an operation at once and the lifetimes of the service InstanceContext relative to incoming channel sessions, respectively.

For details, see MaxConcurrentCalls, and MaxConcurrentInstances.

Vedere anche

Riferimento

ServiceThrottlingBehavior
MaxConnections