Developing Self Hosted WCF service

By | May 30, 2016

In this article I will discuss about the WCF service using C#. Later we will be creating self hosted WCF service from scratch. In this article I will not use the templates provided by the Visual Studio to create WCF service. Instead of that I will create the application from scratch to get the better understanding. You can visit my previous article about WCF contracts here.

Become an Expert in Programming and Web Development.View all courses!

WCF Service project Structure

The WCF project structure for my article is as shown below. I will discuss various parts one by one. I have created this structure for my project to keep all the components of the project separate from each other.


WCF Service

All of the assemblies in this project are ClassLibrary project apart from Client and ServiceHostApp which are console projects.

WCF expert Miguel Castro writes in one of his article here (under The Critque section) the benefits of going with this approach of creating these parts manually.

If the developer and consumer of the WCF service is same organization it is always better to go this way. We can save ourselves from pain of understanding the proxies generated if we consume the service.

Creating Service Contract Project

I start by creating the Contract project which I have named as MyService.Contract. This will contain both my service contract and data contract. I need to add the System.ServiceModel and System.Runtime.Serialization references to it.

The code for the Service contract and data contract is as shown below. It is generally good to end the DataContract with “Data” suffix as PersonData.

Service Project

In this project I have a class named MyServiceImplementation.cs which implements IMyService interface as shown in the code below.This project will have a reference of the service contract’s assembly.

Hosting Application

ServiceHostApp is the hosting project in my solution. It is a console application. If references both the service contract and service projects. It also need to have System.ServiceModel and System.Runtime.Serialization assemblies. The code for service host is as following.

This project also need to have a app.config file where we should expose the service hosted by this application. To expose the service to the client we need to expose the address. binding and contract. These are generally called the ABC of the WCF service. Once the host is opened the service or the host is listening and the host instantiates the service class.

address binding and contract

In the above figure we can see that in serviceModel section we have defined an endpoint. This endpoint is per service. Each service can have many endpoints Endpoint contains the ABC of the service.

The name is the fully qualified name of the Service class. Contract is the fully qualified name of the contract which service implements. I have used the basicHttpBinding. The address of the service is http://localhost:8080/MyService URL. This is the URL where service will be hosted.

The host will keep listening unless we do not press any key as seen in the above code.

Service Proxy Project

In my previous article I have discussed about WCF proxies. Here I have developed a class library project named MyService.Proxies which references the Contract project. Lets have a look at the service proxy code. I will discuss about proxies in details in my next article.


To access the service expose by console host, we have to add the address binding and configuration in the Client App.config as shown in the code below. Add the serviceModel tag under configuration.

Add the reference for the Proxies project and the Contract project which we have created earlier to client. The contract reference is added to have access to the DataContract. Access the service using proxy as shown in the C# code below.

Now we will run the whole solution by setting Multiple Startup project as shown below figure.


After executing the project the output is “Hello Vikram” which is expected from service.


I have created an WCF service without using the visual studio template. To work with WCF service is very easy of we are clear with basics. Please find the code for this article here which Is developed in VS 2015 . Open the VS with administrator privileges.