Service Throttling Behavior in WCF
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
ServiceThrottlingBehavior exposes three properties :
MaxConcurrentSessions
MaxConcurrentCalls
MaxConcurrentInstances
MaxConcurrentSessions
Specifies maximum number of sessions a service host accepts. The default is 10.
MaxConcurrentCalls
Gets or sets a value that specifies the maximum number of messages actively processing across a ServiceHost.The default is 16.
MaxConcurrentInstances
Gets or sets a value that specifies the maximum number of InstanceContext objects in the service that can execute at one time.The default is 26.
These values are set in application configuration file :
<serviceThrottling
maxConcurrentCalls="1"
maxConcurrentSessions="1"
maxConcurrentInstances="1"
/>
Programmatic Throttling
ServiceHost host = new ServiceHost(typeof(MyService));
ServiceThrottlingBehavior throttle;
throttle = host.Description.Behaviors.Find
if(throttle == null)
{
throttle = new ServiceThrottlingBehavior();
throttle.MaxConcurrentCalls = 12;
throttle.MaxConnections = 34;
throttle.MaxInstances = 56;
host.Description.Behaviors.Add(throttle);
}
host.Open();
Reading Throttled Values :
class MyService : ...
{
public void MyMethod() //Contract operation
{
ChannelDispatcher dispatcher = OperationContext.Current.Host.
ChannelDispatchers[0] as ChannelDispatcher;
ServiceThrottle serviceThrottle = dispatcher.ServiceThrottle;
Trace.WriteLine("MaxConcurrentCalls = " +
serviceThrottle.MaxConcurrentCalls);
Trace.WriteLine("MaxConnections = " +
serviceThrottle.MaxConnections);
Trace.WriteLine("MaxInstances = " + serviceThrottle.MaxInstances);
}
}
No comments:
Post a Comment