void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase) { // Create an instance of the ObjectPoolInstanceProvider. ObjectPoolingInstanceProvider instanceProvider = new ObjectPoolingInstanceProvider(description.ServiceType, minPoolSize); // Forward the call if we created a ServiceThrottlingBehavior. if (this.throttlingBehavior != null) { ((IServiceBehavior)this.throttlingBehavior).ApplyDispatchBehavior(description, serviceHostBase); } // In case there was already a ServiceThrottlingBehavior (this.throttlingBehavior==null), // it should have initialized a single ServiceThrottle on all ChannelDispatchers. As // we loop through the ChannelDispatchers, we verify that and modify the ServiceThrottle // to guard MaxPoolSize. ServiceThrottle throttle = null; foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers) { ChannelDispatcher cd = cdb as ChannelDispatcher; if (cd != null) { // Make sure there is exactly one throttle used by all endpoints. // If there were others, we could not enforce MaxPoolSize. if ((this.throttlingBehavior == null) && (this.maxPoolSize != Int32.MaxValue)) { if (throttle == null) { throttle = cd.ServiceThrottle; } if (cd.ServiceThrottle == null) { throw new InvalidOperationException(ResourceHelper.GetString("ExNullThrottle")); } if (throttle != cd.ServiceThrottle) { throw new InvalidOperationException(ResourceHelper.GetString("ExDifferentThrottle")); } } foreach (EndpointDispatcher ed in cd.Endpoints) { // Assign it to DispatchBehavior in each endpoint. ed.DispatchRuntime.InstanceProvider = instanceProvider; } } } // Set the MaxConcurrentInstances to limit the number of items that will // ever be requested from the pool. if ((throttle != null) && (throttle.MaxConcurrentInstances > this.maxPoolSize)) { throttle.MaxConcurrentInstances = this.maxPoolSize; } }