Web application developer

Creating a basic service in WCF


Written by: willem | Jan 30 2014 9:40 PM

For a project I did recently we were using devices that were connected with our server through a TCP connection. Every time we released a new version of the server software we had to restart the service which caused a disconnect event in all devices. Then, when the devices rebooted, they all try to connect at the same time, giving the service a hard time setting up all the connections.

To prevent this disconnection we decided to introduce a connection layer in the architecture. This layer has only two simple tasks: maintaining the connection with the devices and routing all the data that is sent directly through a WCF connection to the business layer.

This WCF connection is mainly a named pipes connection (shared memory), but during an update of this WCF service it would fall back through TCP to a redundant service running on another server. This way we can actually update most of the service software without loosing existing connections to the devices.

So, for this architecture I needed to find out how WCF services actually work, and how you can support named pipes and TCP at the same time. Next to that, the system admin preferred windows services instead of running the services in IIS. I had some problems finding out these things, especially how to host the services so I decided to make some blog posts about it and explain how to create, configure and host a WCF service in different ways.

I will show you step by step how to make a simple WCF service that processes messages. The service will run on a http port, tcp port and through named pipes. It will have one method: ProcessMessage(). This method will return the string that is sent and optionally reverse the characters. I will explain this in a series of blog posts.

I will start with showing you how to create a basic WCF service.

Start visual studio as administrator and create a new project.


Name the project MessageService.


Visual studio creates a project from a template which has already two basic files present. One is called IService and one is the corresponding class Service1.

A WCF service consists of three parts:

  • Service contract, this is an interface defining the methods or properties of the service
  • Data contract, this can be any class. If you want to send or receive data of a type of a custom class it should be defined as a data contract.
  • Implementation of the service. This class implements the service contract. This class contains the methods and properties that are actually called when using the service.

First rename the file IService.cs to IMessageService.cs and Service1.cs to MessageService.cs. If visual studio asks to update the references in the project, click yes.





Next open the file IMessageService.cs and replace the code into this:

	using System.Runtime.Serialization;
	using System.ServiceModel;

	namespace MessageService
	    public interface IMessageService
	        string ProcessMessage(Message message);

	    public class Message
	        public bool Reverse { get; set; }

	        public string StringValue { get; set; }

You just created an Interface and a class.


The interface IMessageService describes what is available in the service. It is a contract the service is providing. You can see this literally as an interface since clients that use the service will have access to the functionality the interface describes. To tell WCF that this interface is a service contract you add the annotation [ServiceContract]. Next for every method or property you want to add to the service contract you add the annotation [OperationContract].

The class Message has the annotation [DataContract]. This tells WCF that this class can be used to send or receive through methods in the service contract. Every property of that will be used for this should have a the [DataMember] annotation.

So in our case we have a IMessageService service contract that provides the method ProcessMethod(Message message). Since this method receives a message of the type Message, we defined this class as a DataContract.

Next open MessageService.cs and replace the code for this:

	using System.Linq;

	namespace MessageService
	  public class MessageService : IMessageService
	    public string ProcessMessage(Message message)
	      if (message.Reverse)
	        char[] stringValue = message.StringValue.ToArray();
	        return new string(stringValue.Reverse().ToArray());
	        return message.StringValue;

In this class we implement the IMessageService, which means it has a method called ProcessMessage. The implementation of this method actually defines how the ProcessMessage of the IMessageService is acting. In this case it checks the boolean property Reverse of the sent message and if true it reverses and returns the StringValue of the message. If it is false it just returns the StringValue.

Now the code is ready, but we need to change the configuration of the service. Open App.Config and change the baseAddress under host into this, but don’t change the port if it is different:

    <add baseAddress="http://localhost:8733/MessageService/" />

Next configure the end point:

	<endpoint address="" binding="basicHttpBinding" 

In later posts I will explain more about the configuration. For now, start debugging the service.


Visual studio will start up automatically the WCF test client. With this little program you can test the method you just created.


Click ProcessMessage() on the left side and expand message on the right. The WCF test client analysed the MessageService and lets you run its methods. By expanding the message field, you can edit its properties.


Set the Reverse field to True, the StringValue to some text and click the invoke button.


As you can see the StringValue is returned in reverse order.


Download the solultion here.

This was it for now. In my next blogpost I will explain how you connect a client to the service you just created.

No Comments

Add a Comment

About Me

I'm a web application developer specializing in asp.net. I have skills in asp.net, C#, html5, javascript, asp.net mvc, design patterns and more.

more about me

Random background module

This module for Orchard CMS lets you upload a list of images. For every page it chooses one randomly and sets it a the background image.

Go to project page on CodePlex
Download module

The Monack Framework

This open source framework makes it easy to build and develiver custom web applications. Unfortunately I can't spent much time on it so it is still work in progress. 

More about MonackFr
Project page at CodePlex


Since I decided to create this website in Orchard and didn't know anything about
it when I started, I also post my experiences with this CMS.