|Read more about this book|
(For more resources on Microsoft, see here.)
Service hosting and configuration is very important for building WCF services, especially at the service deployment stage. After developers complete the service development, we will need to deploy the service so as to make it available to all the client consumers. In the real world, there are various service deployment scenarios available, which will result in different deployment and configuration requirements on the service configuration or the hosting environment.
As an enhanced service development platform, WCF provides rich, built-in support on service hosting and configuration that can fulfill most of the existing deployment demands and requirements. For example, the most popular IIS hosting approach can provide high availability and stable service for local intranet or public internet-based deployment cases. The Windows service-hosting approach makes WCF service hosting easier to integrate with existing background scheduled tasks, and the self-hosting approach provides the most flexibility and customization points for service deployment in a production environment.
In this article, we will look at seven recipes on various WCF hosting and configuration scenarios. The recipes start with four typical hosting cases—self-hosting, Windows service hosting, IIS-based HTTP hosting, and IIS based non-HTTP hosting. This is followed by two customized service-hosting cases—including a custom ServiceHostFactory and a dedicated singleton-instance hosting. The last recipe demonstrates a more advanced WCF service-hosting scenario—Windows SharePoint Service hosting.
Hosting a service in a console application
When creating a simple demo program for .NET framework, we will probably choose a console application. At the same, when talking about WCF service hosting, the console-hosting scenario is the most convenient one, which is especially handy and useful when we want to do some quick demo or testing on some WCF functionality.
How to do it…
- Create a .NET framework-based Console project through Visual Studio.
Visual Studio provides various project templates for creating a .NET framework-based application. For our sample console-hosting service here, we will choose the Console Application project type from the Visual Studio New Project wizard.
- Add a new WCF service into the project.
We can simply accomplish this by using the Add New Item function in Visual Studio and choose WCF Service as the item type from Visual Studio’s Add New Item UI.
- Add code into the Main function to start up the WCF service. The following code shows the typical Main function that starts up a WCF service in a console application: static void Main(string args)
using (ServiceHost consoleHost = new
Console.WriteLine("press any key to stop service host...");
How it works…
When you add a new WCF Service item in Visual Studio, the IDE actually helps you to finish the following three tasks:
- Creating a ServiceContract interface.
- Creating a Service class that implements the ServiceContract interface.
The following code shows the sample ServiceContract and implementation class used in this recipe.
public interface ITestService
[OperationContract] void DoWork();
public class TestService : ITestService
public void DoWork()
- Adding the service endpoint and binding configuration in the App.config file.
In addition to the Contract and service type, the IDE will also insert a default configuration setting for the endpoint that can be exposed through the service. The following screenshot shows the sample service configuration section that contains a single endpoint, which uses WSHttpBinding.
With the code and configuration entries as defined previously, we can start our service host by supplying the service type in the constructor of the ServiceHost class.
using (ServiceHost consoleHost = new
What the runtime will do is, it will lookup the configuration file and load the <service> entry that has the name identical to the type specified in the constructor, and launch the service and endpoints defined in it.
The source code for this article can be found at http://www.packtpub.com/code_download/6034
Hosting a service in Windows Service
Windows Services are widely used on Windows operating systems for hosting applications that will perform some long-run or scheduled tasks in the background. Applications hosted via Windows Service can be running under a specific user account and can choose the startup mode (manually or automatically). As a popular service-application-hosting scenario, it is also quite common to deploy a WCF service as a Windows Service.
How to do it…
In this recipe, we will use a typical .NET-based Windows Service to demonstrate how to host a WCF service in a Windows Service application. Let’s go through the detailed steps:
- Create a Windows Service project.
The first step is to create a new Windows Service project through the Visual Studio IDE. When creating the project, we simply choose the Windows Service project type. The following screenshot shows how we can select the Windows Service project type in the Visual Studio New Project wizard.
- Add a new WCF service item.
As a WCF service hosting application, we certainly need to have a WCF service defined here. The steps for creating a WCF service are the same as what we’ve discussed in the Hosting a service in console application recipe.
- Add service hosting code into the service startup and shutdown event.
As for the service-hosting code in the Windows Service, we need to put it in the correct place, since the .NET-based Windows Service type doesn’t directly expose the Main function. The following code shows how the WCF service startup and shutdown code is defined:
public partial class Service1 : ServiceBase
ServiceHost _svcHost = null;
protected override void OnStart(string args)
// Start the service host here
_svcHost = new ServiceHost(typeof(TestService));
protected override void OnStop()
// Close the service host
- Add an installer for the Windows Service.
Now the Windows Service class and WCF service types have been defined. However, we still need to add another component—the installer class for deploying the Windows Service into the Windows Service collection on the target operating system. In the Visual Studio IDE, we can simply add an installer type for the Windows Service by the context menu on the component designer. The following screenshot shows the context menu item for creating the installer class for the Windows Service.
The IDE will help create two helper classes—one is of ServiceProcessInstaller type and another of ServiceInstaller type. We can specify many deployment parameters for the Windows Service in the Property panel of the two classes. The following screenshot shows the properties of the sample serviceProcessInstaller1 class.
The next screenshot shows the properties of the sample serviceInstaller1 class.
As with the screenshots displayed, Visual Studio will use standard Properties windows for displaying and configuring the individual properties of the Windows Service classes.
- Install the Windows Service through Installutil.exe.
The last step is to install the Windows Service we have created (after building the project) into the operating system. This can be done by using the Installutil.exe tool provided by the .NET framework. You can directly execute the Installutil.exe command within the Visual Studio command-line prompt window or you can choose to launch the tool through its absolute path in the .NET framework folder such as C: WindowsMicrosoft.NETFrameworkv4.0.30319.
The following statements show the complete commands for installing and uninstalling a .NET-based Windows Service application via the Installutil. exe tool.
Install the Windows Service:
Uninstall the Windows Service:
Install Util.exe /u WCFNTService.exe
The WCFNTService.exe mentioned earlier is the output assembly name of the sample Windows Service project.
How it works…
The OnStart event is fired when the Windows Service is starting, while the OnStop event is fired when the Windows Service is shutting down. Therefore, they are the best places for us to put the WCF service-hosting code.
Sometimes, we may need to access some remote or protected resource in our Windows Service host program. In such cases, it is important to specify a proper service account, either at development time or in the Windows Service Configuration Manager. The following screenshot shows the service list, which contains the installed sample Windows Service in Windows Service Configuration Manager.
- Hosting a service in a console application