The WCF service class cannot exist in a void. Every WCF service must be hosted in a Windows process called the host process
. A single host process can host multiple services and the same service type can be hosted in multiple host processes. WCF makes no demand on whether or not the host process is also the client process.
Obviously, having a separate process advocates fault and security isolation. It is also immaterial who provides the process or what kind of a process is involved. The host can be provided by IIS, by the Widows Activation Service (WAS) on Windows Vista, or by the developer as part of the application.
The main advantage of hosting a service in IIS is that the host process is launched automatically upon client requests, and you rely on IIS to manage the lifecycle of the host process. The main disadvantage of IIS hosting is that you can only use HTTP for transport on both IIS5 and IIS6; when using IIS5, you can only rely on port 80. Hosting in IIS is very similar to hosting a classic ASMX Web service. You need to create a virtual directory under IIS and supply a .svc file. The .svc
file functions like an .asmx
, and is used to identify the service code-behind file and class.
Language = "C#"
Debug = "true"
CodeBehind = "~/App_Code/MyService.cs"
Service = "MyService"
You can even inject the service code in-line in the .svc
file, but that is unadvisable, as is the case with ASMX. Once you have the .svc
file in place, you can navigate to it using a browser. If all is well, you will get a confirmation page.
Visual Studio 2005 can generate a new IIS-hosted service for you. From the File menu, select New Website and then select WinFX Service from the New Web Site dialog box. This causes Visual Studio 2005 to create a new Web site, service code, and matching .svc file. In addition, the Web site config file must list the types you want to expose as services. You need to use fully-qualified type names, including assembly name, if type comes from an unreferenced assembly.
is the name for the technique used when the developer is responsible for providing and managing the lifecycle of the host process. Self-hosting is used both in the case of wanting a process (or computer) boundary between the client and the service and when using the service in-procthat is, in the same process as the client. The process you need to provide can be any Windows process, such as a Windows Forms application, a Console application, or a Windows NT Service. Note that the process must be running before the client calls the service, which typically means you have to pre-launch it. This is not an issue for NT Services or in-proc.
Similar to IIS hosting, the hosting application config file must list the types of the services you wish to host and expose to the world. In addition, the host process must explicitly register the service types at run time and open the host for client calls. This is typically done in the Main()
method using the helper class ServiceHost defined as:
public interface ICommunicationObject : IDisposable
public abstract class CommunicationObject :
public class ServiceHostBase : CommunicationObject,...
public class ServiceHost : ServiceHostBase,...
public ServiceHost(Type serviceType,
Provide the constructor of ServiceHost with the service type and optionally with default base addresses. The set of base addresses can be an empty set and later, you can configure different base addresses. Having a set of base addresses enables the service to accept calls on multiple addresses and protocols. Note that each ServiceHost instance is associated with a particular service type, and if the host process needs to host multiple types of services, you will need a matching number of ServiceHost instances. By calling the ServiceHost.Open()
method on the host, you allow calls in, and by calling the ServiceHost.Close()
method, you gracefully exit the host instance and finish sending data to current clients and yet refuse future client calls even if the host process is still running. Closing is typically done on host process shutdown. For example, to host this service in a Windows Forms application:
class MyService : IMyContract
You can write:
public static void Main()
Uri baseAddress = new
serviceHost = new
//Can do blocking calls:
Note that you can do blocking calls after the call to ServiceHost.Open()
because the host accepts calls on worker threads. The call to ServiceHost.Open()
loads the WCF runtime and enables accepting client calls. The host can register multiple base addresses as long as they differ at least by the transport:
Uri tcpBaseAddress = new
Uri httpBaseAddress = new
ServiceHost serviceHost = new
Visual Studio 2005 allows you to add a WCF service to any application project by selecting WCF service from the Add New Item dialog box. The service added this way is, of course, in-proc toward the host process, but can be accessed by external clients as well.
The Windows Activation Service (WAS) is a system service available with Windows Vista. WAS is part of IIS7 but can be installed and configured separately. To use the WAS for hosting your WCF service, you need to supply a .svc file just as with IIS. The WAS offers many advantages over IIS and self-hosting including idle time management, identity management, application pooling, and isolation, and is the host process of choice when available. Still, the self-hosted process offers singular advantages for in-proc hosting, dealing with unknown customer environment, relying on TCP or IPC when only IIS is available, utilizing multiple ports on HTTP when only IIS5 is available, and programmatic access to some advanced hosting features.