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.