Every WCF service needs to 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 services can be hosted in many ways:
IIS 5/6 Hosting
WCF services can be hosted in IIS. It is similar to hosting a ASMX web service. To host a WCF service, you need to create a .svc file similar to this example and put it in virtual directory of IIS:
<%@ ServiceHost Language = "C#" Debug = "true" CodeBehind = "˜/App_Code/MyService.cs" Service = "MyService"%>
Instead of defining a .svc file, you can create and host the service in .config file as below:
<system.serviceModel> <serviceHostingEnvironment> <serviceActivations> <add relativeAddress = "MyService.svc" service = "WcfService.MyService"/> <add relativeAddress = "MyOtherService.svc" service = "MyOtherService"/> </serviceActivations> </serviceHostingEnvironment> <services> <service name = "WcfService.MyService"> ... </service> <service name = "MyOtherService"> ... </service> </services> </system.serviceModel>
Self Hosting
Self-hosting is the technique in which the developer is responsible for providing and
managing the lifecycle of the host process. You can host WCF service inside any Windows process, such as a Windows Forms application, a WPF application, a Console application, or a Windows NT Service.
You can also host your WCF service as in-proc. In-process (or in-proc) hosting is the hosting technique where the service resides in the same process as the client. By definition, the developer provides the host for the in-proc hosting.
var host = new ServiceHost(typeof(MyService)); host.Open(); Console.WriteLine("Press any key to stop service"); Console.ReadKey(); host.Close();
WAS (Windows Activation Service) Hosting
Microsoft provides a generalpurpose hosting engine called the Windows Activation Service (WAS). WAS is a system service available with Windows Vista, Windows Server 2008, and Windows 7 (or later). The WAS is a true general-purpose hosting engine. It can host websites (in fact, IIS 7 will host its websites in the WAS by default), but it can just as easily host your services, allowing you to use any transport, such as TCP, IPC, or MSMQ. You can install and configure the WAS separately from IIS 7. Hosting a WCF service in the WAS is designed to look just like hosting in IIS 5/6. You need to either supply an .svc file, just as with IIS 5/6, or provide the equivalent information in the config file.
Custom Hosting in IIS/WAS
When using IIS 5/6 or WAS, you have no direct access to the host. To overcome this hurdle, WCF provides a hook called a host factory. Using the Factory tag in the .svc file, you can specify a class you provide that creates the host instance.
<%@ ServiceHost Language = "C#" Debug = "true" CodeBehind = "˜/App_Code/MyService.cs" Service = "MyService" Factory = "MyServiceFactory" %>
You can also specify the host factory in the config file when not using an .svc file explicitly:
<serviceActivations> <add relativeAddress = "MyService.svc" service = "MyService" factory = "MyServiceFactory" /> </serviceActivations>
Note: The host factory class must derive from the ServiceHostFactory class and override the CreateServiceHost() virtual method.
Windows Server AppFabric
AppFabric is an extension to the WAS. It is geared more toward WF services, which require support for persistence and state management correlation. Windows Server AppFabric adds items for managing and monitoring the services, as well as WCF and WF configuration items, to the IIS 7 management console. Windows Server AppFabric provides a dashboard for monitoring the running instances of WCF or WF services, and is reminiscent of the MTS or COM+ Component Services Explorer of old. Windows Server AppFabric provides health monitoring and custom diagnostics, as well as some troubleshooting features for analyzing why a service call has failed.
Windows Server AppFabric also supports scripting of all the options available in the user interface. Windows Server AppFabric offers its own events collecting system service, which stores the events in a SQL Server database. You can provide Windows Server AppFabric with tracing and tracking profiles at various verbosity levels.