jump to navigation

Host WCF Services in Window Services March 4, 2012

Posted by fofo in C#, Visual Studio 2008, Visual Studio 2010, WCF, WCF Service.
Tags: , ,

In this post I will show you how to host a WCF Service in a Windows service.This is the second post in a series of posts on the various options we have when it comes to hosting WCF Services.If you want to know how to host the WCF Service in IIS have a look in this post.

I will use a simple a WCF service that just completes some basic math tasks.The main focus in this post is not to implement a very complicated WCF Service. The main focus is demonstrating how to host the WCF Service in IIS.

Let’s build the WCF Service first.

1) Launch Visual Studio. From the available templates use “WCF Service Library”. Choose a suitable name for that project.I have named mine WCFCalculatorService. I will use C# as the development language.

2) Rename the IService.cs and Service.cs to ICalculator.cs and Calculator.cs

3) I will not be deleting anything from the existing code in those files. I will add some code in the ICalculator.cs

public interface ICalculator

double Add(double a1, double a2);

double Multiply(double a1, double a2);

string GetData(int value);

CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here

// Use a data contract as illustrated in the sample below to add composite types to service operations
public class CompositeType
bool boolValue = true;
string stringValue = "Hello ";

public bool BoolValue
get { return boolValue; }
set { boolValue = value; }

public string StringValue
get { return stringValue; }
set { stringValue = value; }

4) Now we need to implement the methods we wrote in the ICalculator.cs file.

public class Calculator : ICalculator

        public double Add(double n1, double n2)
            return n1 + n2;

        public double Multiply(double n1, double n2)
            return n1 * n2;

        public string GetData(int value)
            return string.Format("You entered: {0}", value);

        public CompositeType GetDataUsingDataContract(CompositeType composite)
            if (composite == null)
                throw new ArgumentNullException("composite");
            if (composite.BoolValue)
                composite.StringValue += "Suffix";
            return composite;

5) Build and run your application.Visual Studio will launch the WCF Test Client  so we can test the service.The service will be hosted in the WCF Service Host application that VS launches automatically.Test that the service works as expected.

6) Now we need to add a new project to our solution.I will add a Windows Service project and will name it WindowsServiceHost.This is the project that will host our WCF Service.

7) In the Service1.cs[Design] hit F4 to make the Properties window visible – and change the Name and ServiceName properties to CalculatorServiceHost.

8) We need to add some references to the Windows Service project – WindowsServiceHost. We need to add references to the System.ServiceModel and to the WCF Service Library project –WCFCalculatorService.

9) Let’s write some code inside the Service1.cs file.

public partial class CalculatorServiceHost : ServiceBase

        ServiceHost host = new ServiceHost(typeof(Calculator));

        public CalculatorServiceHost()

        protected override void OnStart(string[] args)

        protected override void OnStop()

We construct a ServiceHost instance based on the Calculator Service class.Then we need to open the host ( host.Open() ) in the OnStart() method.We also need to close the host in the OnStop method ( host.Close() )

10) Then we need to add an app.config file in the WindowsServiceHost project.Inside this app.config file we will have the endpoints definition that is absolutely necessary.We copy the app.config from the WCFCalculatorService to the WindowsServiceHost project.

11) Now we need to add an installer to our project. We go to the Service1.cs[Design] right-click and choose “Add Installer”

12) In the ProjectInstaller.cs [Design] we select the service serviceProcessInstaller1 and hit F4 to show the properties window.Choose the Account property and set it to the LocalSystem.

13) Now we need to build the project and then we need to install and test the windows service.I will launch a Visual Studio 2010 command prompt window as an Administrator and then I will move to the WindowsServiceHost folder.In my case I type in the command prompt cd for blog\WCFCalculatorService\WindowsServiceHost\bin\Debug and hit Enter.Then I type

installutil WindowsServiceHost.exe”.Hopefully after that the service is installed.Go to the Services snap-in from the Control Panel->Administrative Tools.You will see the service CalculatorServiceHost (in my case) installed and you need to start it.

Have a look at the picture below

12) Now we have the WCF service hosted and we can get to its WSDL definition by opening a web browser and type in the URL address “http://localhost:8732/Design_Time_Addresses/WCFCalculatorService/Service1/“. In my case it works just fine and it provides me with the WSDL definition of the service.If you stop the service from the Services console and hit CTRL+F5 in the browser window then you will fail. So it is quite clear now that the service is hosted in the Windows service.

13) Now we can test the WCF service from the test client.Open a Visual Studio command prompt window and type

wcftestclient http://localhost:8732/Design_Time_Addresses/WCFCalculatorService/Service1/ and hit Enter. It is going to talk to our service and find its metadata.It is going to launch the familiar test client so we test the service.Test the WCF Service to see if it works as expected.

Have a look at the picture below to see what I mean.

We managed to host a WCF Service in a Windows Service and test its functionality by using the WCF Test Client provided by VS.

Email me if you need the source code.
Hope it helps!!!!!



1. Dot Net Rules : Host WCF Services in Window Services - March 4, 2012

[…] a very complicated WCF Service. The main focus is demonstrating how to host the WCF Service in IIS.(read more) Share Posted: Κυριακή, 4 Μαρτίου 2012 2:17 πμ από το μέλος […]

2. How to configure WAS for TCP endpoints in IIS « DOT NET RULES - March 5, 2012

[…] TCP endpoints. Recently I have posted 3 posts on WCF hosting options. You can find them here , here and […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: