David Pallmann’s WCF Tips #3 - Service Hosting
Tip: Consider IIs and non-IIS hosting for services rather than just using the method more familiar to you. Leverage IIS hosting if you need its features (such as health monitoring) and your customers are willing to deploy IIS. Use direct hosting, such as in a Windows Service Application, if more appropriate. Note: the author hosts WCF services in Windows Services nearly all the time.
Rationale: Previous Microsoft communication technologies tend to have a single hosting model, but WCF services can be hosted either in IIS or by the developer’s own code (essentially from any managed code environment). Developers should be aware of their hosting options and use the most appropriate one for their scenario.
IIS-hosted services provide a number of compelling features, including automatic activation, health monitoring, process recycling, compile-on-demand, and the ability to update the application or its config while still running. IIS also makes it easy to accomplish some things that are hard to do in code, such as supporting SSL. However, IIS also has some limitations. Not all customers are willing to deploy it. You are limited to the HTTP/S transport (unless using IIS 7.0). And the periodic process recycling that IIS/ASP.NET undergoes can sever a client’s session and security content unexpectedly.
Hosting a service yourself gives you more control over the lifetime of the service, but at the expense of the aforementioned IIS feature s unless you have an alternative implementation. Many developers and I.T. personnel find using a Windows Service Application more attractive. Neudesic provides a universal service host implemented in a Windows Service Application container, written by Chris Rolon.
What if I Don’t? You may be taking the hard way if you aren’t familiar with all of your options.
Examples:
· A service is being created that is public-facing and must use HTTPS. The developer chooses to host the service in IIS because it is easiest to set up the SSL certificate that way.
· A service is being created that has a dependency on several other services running on the same machine. The developer decides to host the services in Windows Service Applications so that the Windows Service Controller can enforce the dependencies.
Tip: If hosting your service directly, configure your ServiceHost object. Override the debilitating default properties so that you can get real work done.
Rationale: In the name of shipping “securely by default”, WCF Services have great limitations on the amount of work they can perform. To get real levels of work done in the enterprise requires overriding many default values. Some of these values are in the ServiceHost object, while others apply to the binding of endpoints.
Of course, these limits were set with good intentions such as preventing denial of service attacks. Don’t override the ServiceHost properties with values larger than you need.
What if I Don’t? You’ll run into limitations at runtime, such as not being able to support more than 10 clients at a time.
Example: The author routinely initializes a ServiceHost object this way:
// Set throttling permissions to allow many concurrent service connections.
public static void SetServiceHostThrottle(ServiceHost host)
{
ServiceThrottlingBehavior throttle;
throttle = host.Description.Behaviors.Find<ServiceThrottlingBehavior>();
if (throttle == null)
{
throttle = new ServiceThrottlingBehavior();
throttle.MaxConcurrentCalls = 1000;
throttle.MaxConcurrentInstances = 1000;
throttle.MaxConcurrentSessions = 1000;
host.Description.Behaviors.Add(throttle);
host.CloseTimeout = TimeSpan.FromSeconds(1);
}
}