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).
http://localhost:8000/ServiceModelSamples/service
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 127.0.0.1:8000”, 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);
Console.ReadKey();
}
}
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:
try
{
// Establish the connection to the Service
var calculatorClient = new CalculatorClient();
var firstOperand = Convert.ToDouble(FirstOperand);
var secondOperand = Convert.ToDouble(SecondOperand);
var stopwatch = new Stopwatch();
stopwatch.Start();
switch (Operation)
{
case ("+"):
result = calculatorClient.Add(firstOperand, secondOperand).ToString();
break;
case ("-"):
result = calculatorClient.Subtract(firstOperand, secondOperand).ToString();
break;
case ("*"):
result = calculatorClient.Multiply(firstOperand, secondOperand).ToString();
break;
case ("/"):
result = calculatorClient.Divide(firstOperand, secondOperand).ToString();
break;
}
stopwatch.Stop();
And, when we run the calculator, it looks like this:
Conclusions
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.










June 19th, 2010 at 2:27 pm
Thanks, It saved me alot
June 20th, 2010 at 6:55 pm
Very nice. Thanks
June 22nd, 2010 at 7:18 am
Thanks for the samples. Helped tremendously.
August 23rd, 2010 at 4:40 am
Thank you so much. Very good.
September 7th, 2010 at 4:47 am
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.
September 20th, 2010 at 2:23 pm
Serge, Try this:
Right click on the Service References/ServiceReferenceCalculator in both the Calculator and ConsoleApplication Projects and select
HTH,
George
October 28th, 2010 at 1:30 pm
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?
December 10th, 2010 at 12:48 pm
@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.
-Niranch
December 30th, 2010 at 7:52 am
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?
March 2nd, 2011 at 1:17 am
Thanks for the simple samples. Very good
March 7th, 2011 at 9:02 am
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?
Cheers.
March 7th, 2011 at 9:19 am
Found it – I had to update the service reference in both the calculator and the console app.
Works great thanks.
March 27th, 2011 at 2:23 am
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.
Thanks
May 10th, 2011 at 1:36 pm
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.
June 20th, 2011 at 4:18 am
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:
“Services
—————————
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?
Thanks
August 9th, 2011 at 8:48 am
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
October 27th, 2011 at 12:48 pm
Nicely done! Appreciate the work on this.
November 25th, 2011 at 10:15 am
Hi Peter,
Great example! Great Work!
I just had to update service references and it worked.
Thank you very much.
Eliseu