A VS2010 Project Made From Post: How to: Host a WCF Service in a Managed Windows Service

MSDN has a very nice article on how to create a windows service that hosts a Windows Communication Foundation (WCF) service.  It explains all the details of doing this in a step by step fashion.  One thing that I often find missing from these articles is the actual Visual Studio project that I can download and play with.  What I usually do is put that together myself (which I’m sure is the author’s intent).

To save anyone some time who wants to do the same thing, I’ve created a VS2010 project from the example, added a very simple Windows C# console application that consumes the service, as well as made some small changes in a very nice Windows Presentation Foundation (WPF) calculator project so that the calculator does it operations inside the windows service rather than in the calculator itself.

In this article, I’ve attached the source code (with my small changes and additions) for you to work with and change as you like.

First, here is the project:

Visual Studio 2010 Project Project Zip Here

Now, let’s talk about the details


The Visual Studio 2010 Solution Itself


There are three projects in this solution.  The service itself which is called WCFServiceInmanagedWindowsService, Console Application and Calculator.


WCFServiceInManagedWindowsService Project


This project is really what is taken from the MSDN article. It’s got almost no change and is primarily created by following the directions in the article.  There are a couple batch files added for creating and deleting the service itself in the root of that project directory, but that’s about it.  All the code is in a file called service.cs.

To Add the service, go to the “Service Reference”/”Add Service” Dialog and enter the address of the service (you can find it in the app.config file).




Notice that the methods exposed are Add/Divide/Multiple and Subtract.

To start the actual service, after rebuilding the project, execute the bat file InstallService.bat in the root directory.  Make sure you build the release version because this script installs the service from the release directory. Once started, you will see it in the services application as follows:


Then, start the service by running the command: “net start WCFWindowsServiceSample”  If you get the error: “No connection could be made because the target machine actively refused it”, this likely means you did not start your service.


The Console Application

The Console application is new and very simple. All you have to do is create a new windows c# console project, use the “Add Service” DialWCFWindowsServiceSampleog and point it at  ( http://localhost:8000/ServiceModelSamples/service ) as follows:


Now, you can simply write a console app with the following code and you will be calling the service correctly.  Here is the code:


class Program
static void Main(string[] args)
var calculatorClient = new CalculatorClient();

var answer = calculatorClient.Add(5, 4);
Console.WriteLine("Answer to Adding 5 + 4: {0}", answer);


And, when we run it, now surprise, we get 9!


The Calculator Application

Just to show a real life use of the service, I grabbed the codeplex project http://code.google.com/p/wpf-mvvm-calculator/.


Then, modifying a small section of code inside (after adding the service reference of course, just like we did in the above console project, we now have a calculator that adds by calling a service for the answer.  Here is the modified code:

// Establish the connection to the Service
var calculatorClient = new CalculatorClient();
var firstOperand = Convert.ToDouble(FirstOperand);
var secondOperand = Convert.ToDouble(SecondOperand);

var stopwatch = new Stopwatch();

switch (Operation)
case ("+"):
result = calculatorClient.Add(firstOperand, secondOperand).ToString();

case ("-"):
result = calculatorClient.Subtract(firstOperand, secondOperand).ToString();

case ("*"):
result = calculatorClient.Multiply(firstOperand, secondOperand).ToString();

case ("/"):
result = calculatorClient.Divide(firstOperand, secondOperand).ToString();


And, when we run the calculator, it looks like this:




In this post, we simply implemented the source as a Visual Studio 2010 project from the MSDN article on how to build a windows service using WCF.  It’s been pointed out that we are better off using named pipes for this kind of application for better performance, but our purpose here was just to elaborate on the existing application.

Hope this helps.

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP since 2007. In addition, he’s a multi-course video author at Pluralsight. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


  1. Thanks Romain!

  2. I managed to translate it to VB.NET : https://skydrive.live.com/redir?resid=FB6D08E7E846B55D!145

    This version runs just fine.

  3. Yashaswini says:

    Hey, thanks for tht quick reply..
    I am running it as admin and the important thing is that I am not able to browse it 🙁

  4. actively refusing means that for some reason it does not see the server. Is it running as admin? can you browse to it? It’s been a while since I did this so I don’t really remember what might make that happen. It can be tricky. There are WCF diagnostics you can run that help. I again don’t remember where to find them, but they are pretty good.

  5. Yashaswini says:

    Hey i tried the example which was mentioned in msdn site.
    I was successful in hosting the service in windows service. Later I tried to consume it creating a WCF client and was not able to create client code using Svcutil.exe . I tried the other way by creating a console app as client and tried giving the service as ref. But it failed saying :

    There was an error downloading ‘http://localhost:8001/ServiceModelSamples/service’.
    Unable to connect to the remote server
    No connection could be made because the target machine actively refused it
    Metadata contains a reference that cannot be resolved: ‘http://localhost:8001/ServiceModelSamples/service’.
    There was no endpoint listening at http://localhost:8001/ServiceModelSamples/service that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
    Unable to connect to the remote server
    No connection could be made because the target machine actively refused it
    If the service is defined in the current solution, try building the solution and adding the service reference again.

    Can you help me in going further?? I am stuck here.


  6. Eliseu Rodrigues says:

    Hi Peter,

    Great example! Great Work!
    I just had to update service references and it worked.

    Thank you very much.


  7. Nicely done! Appreciate the work on this.

  8. Hi
    Great solution!
    Not sure if this is as intended – but I can only add a service reference to the WCF service once it is installed. I notice that if I use one of the default VS WCF project types, I can “Discover” the service, when trying to use it from another project in the solution.
    Is there a way to make the managed service wcf service discoverable from within the solution without requiring its installation?
    Many Thanks

  9. Hi Peter,

    I’m experiencing the same problem as Aryeh and Jake, I’m able to install the service but I can’t start it.
    I receive the message:
    The MyService service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.

    What do you think is the problem?


  10. Nicolas says:

    Hi Saarthak,

    Try moving your projet to c:\vb2010\…..
    and try it again it should work. We had the same problem here
    and it solve our problem when we did that. I think VS2010 doens’t
    like Windows Documents directory when connected to Active Directory.

  11. Saarthak Vats says:

    Hi Peter,

    I am not able to run the service. When I run InstallService.bat (after I rebuild the project in Release mode ) it shows this error on console –

    Exception occurred while initializing the installation:
    System.BadImageFormatException: Could not load file or assembly ‘file:///C:\User
    s\Saarthak\Documents\Visual Studio 2010\Projects\WCFServiceInManagedWindowsServi
    ce\bin\debug\WCFServiceInManagedWindowsService.exe’ or one of its dependencies.
    An attempt was made to load a program with an incorrect format..

    Do you have any idea?

    I also tried installing it with “net start WCFServiceInManagedWindowsService” then it says “The service name is invalid”

    Please suggest.


  12. Found it – I had to update the service reference in both the calculator and the console app.

    Works great thanks.

  13. thanks for example. I have the service up & runnning but when i call the console app I get:
    System.ServiceModel.Security.SecurityNegotiationException was unhandled
    Message=The caller was not authenticated by the service.

    Anyone any ideas?

  14. Gunnar Thorvaldsen Abrahamsrud says:

    Thanks for the simple samples. Very good

  15. I am having the same trouble as Aryeh. I try to start the service, but it stops immediately. I havent touched anything in this project. I just downloaded it and installed, so the config file should be correct, right? Is there something I need to do to make localhost:8000/ServiceModelSamples/service a valid address?

  16. @Aryeh
    If you are still facing the issue it might be because your app.config endpoint address or namespace is wrong. Verify your config file.

  17. Aryeh Greenberg says:

    I downloaded your c# example and I was able to install it as a service and start it. The vb sample project (http://msdn.microsoft.com/en-us/library/ms733069.aspx) installed but stopped right away after I tried to start it. What could I be doing wrong?

  18. George in Palmer says:

    Serge, Try this:

    Right click on the Service References/ServiceReferenceCalculator in both the Calculator and ConsoleApplication Projects and select


  19. Hi!
    Trying with the ConsoleApplication got the next exception:
    {“The caller was not authenticated by the service.”}
    on line “var answer = calculatorClient.Add(5, 4);”

    What I should do to overcome this problem?
    Thank you.

  20. Thank you so much. Very good.

  21. Thanks for the samples. Helped tremendously.

  22. Steven Li says:

    Very nice. Thanks

  23. Kourosh Saleh says:

    Thanks, It saved me alot


Get every new post delivered to your Inbox

Join other followers: