Hosting WCF service in Windows Service

Posted by DigiMortal on Weblogs See other posts from Weblogs or by DigiMortal
Published on Fri, 24 Aug 2012 06:10:00 GMT Indexed on 2012/08/27 21:40 UTC
Read the original article Hit count: 561

Filed under:

When building Windows services we often need a way to communicate with them. The natural way to communicate to service is to send signals to it. But this is very limited communication. Usually we need more powerful communication mechanisms with services. In this posting I will show you how to use service-hosted WCF web service to communicate with Windows service.

Create Windows service

VS2010: Create Windows service

Suppose you have Windows service created and service class is named as MyWindowsService. This is new service and all we have is default code that Visual Studio generates.

Create WCF service

Add reference to System.ServiceModel assembly to Windows service project and add new interface called IMyService. This interface defines our service contracts.


public interface IMyService



    string SayHello(int value);


We keep this service simple so it is easy for you to follow the code. Now let’s add service implementation:


public class MyService : IMyService


    public string SayHello(int value)


        return string.Format("Hello, : {0}", value);



With ServiceBehavior attribute we say that we need only one instance of WCF service to serve all requests. Usually this is more than enough for us.

Hosting WCF service in Windows Service

Now it’s time to host our WCF service and make it available in Windows service. Here is the code in my Windows service:

public partial class MyWindowsService : ServiceBase


    private ServiceHost _host;

    private MyService _server;


    public MyWindowsService()





    protected override void OnStart(string[] args)


        _server = new MyService();

        _host = new ServiceHost(_server);




    protected override void OnStop()





Our Windows service now hosts our WCF service. WCF service will be available when Windows service is started and it is taken down when Windows service stops.

Configuring WCF service

To make WCF service usable we need to configure it. Add app.config file to your Windows service project and paste the following XML there:


  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />


    <service name="MyWindowsService.MyService" behaviorConfiguration="def">



          <add baseAddress="http://localhost:8732/MyService/"/>



      <endpoint address="" binding="wsHttpBinding" contract="MyWindowsService.IMyService">


      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>





      <behavior name="def">

        <serviceMetadata httpGetEnabled="True"/>

        <serviceDebug includeExceptionDetailInFaults="True"/>





Now you are ready to test your service. Install Windows service and start it. Open your browser and open the following address: http://localhost:8732/MyService/ You should see your WCF service page now.


WCF is not only web applications fun. You can use WCF also as self-hosted service. Windows services that lack good communication possibilities can be saved by using WCF self-hosted service as it is the best way to talk to service. We can also revert the context and say that Windows service is good host for our WCF service.

© Weblogs or respective owner

Related posts about .NET

Related posts about wcf