Hosting Options for WCF Services
When you develop a Windows Communication Foundation (WCF) service, you need to decide how and where it will be hosted. WCF offers several flexible hosting options to suit various application scenarios. This tutorial explores the primary hosting environments available for WCF services in .NET Framework 3.5.
1. Self-Hosting
Self-hosting involves running your WCF service within your own custom application, such as a Windows Forms application, a WPF application, a console application, or a Windows Service. This provides maximum control over the service's lifecycle and execution environment.
Key Advantages:
- Full Control: You manage the service host, its lifecycle, and the underlying resources.
- Customization: Ideal for services that need to integrate deeply with specific application logic or performance monitoring.
- Independent Deployment: The service runs independently of IIS or the Windows Process Activation Service (WAS).
Example (Console Application):
using System;
using System.ServiceModel;
public class MyService : IMyContract
{
public string GetData(string value)
{
return $"You entered: {value}";
}
}
[ServiceContract]
public interface IMyContract
{
[OperationContract]
string GetData(string value);
}
public class Program
{
public static void Main(string[] args)
{
// Create a ServiceHost for the service type and provide the base address.
using (ServiceHost host = new ServiceHost(typeof(MyService), new Uri("http://localhost:8080/MyService")))
{
// Enable the service's metadata behavior.
System.Web.Services.Description.ServiceDescriptionCollection serviceDescriptions = new System.Web.Services.Description.ServiceDescriptionCollection();
host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });
// Open the service.
host.Open();
Console.WriteLine("Service is running at http://localhost:8080/MyService");
Console.WriteLine("Press to exit.");
Console.ReadLine();
// Close the service.
host.Close();
}
}
}
2. Internet Information Services (IIS) Hosting
Hosting your WCF service within IIS leverages the robust infrastructure and management capabilities of the web server. IIS provides features like process management, security, and scalability.
Key Advantages:
- Process Management: IIS handles process recycling and health monitoring.
- Security: Inherits IIS security features (e.g., authentication, authorization).
- Scalability: IIS is designed for high-traffic web applications.
- Simplified Deployment: Deploy your service as an IIS application.
Considerations:
Requires the IIS role to be installed and configured. The WCF service runs within an `aspnet_wp.exe` or `w3wp.exe` worker process.
3. Windows Process Activation Service (WAS) Hosting
WAS is the activation infrastructure introduced with .NET Framework 3.0. It provides a unified activation system for WCF services, similar to IIS but not limited to HTTP. WAS supports activation via protocols like Net.TCP, MSMQ, and named pipes, in addition to HTTP.
Key Advantages:
- Protocol Support: Actively supports a wider range of protocols beyond HTTP.
- Process Activation: Dynamically starts and stops service processes based on incoming messages, optimizing resource usage.
- Integration: Works seamlessly with IIS for HTTP-activated services.
How it Works:
WAS manages the lifecycle of WCF services and can activate them when messages arrive, even if the service process is not currently running. This is particularly beneficial for services that don't receive constant traffic.
Choosing the Right Option
The choice of hosting option depends on your application's requirements:
- For maximum control and custom scenarios, Self-Hosting is often preferred.
- For web-based services that benefit from web server features and scalability, IIS Hosting is a strong choice.
- For services requiring activation across multiple protocols and dynamic process management, WAS Hosting offers flexibility.