When I had a WCF Service, the next step was to host the WCF Service. I was amazed when I noticed that the Class1.cs file (created by visual studio while I was trying to create a WCF Service Library project) has instructions of how to host a WCF service.
They suggest this can be done in 9 steps:
1- Create a Host project. I personally added a new project to the solution that had the WCF service. To do this you right click the solution, select add, select new project, and then I choose a Console Application project.
2- Add to your host project a CodeFile, which is a blank C# file.
3- In this blank codefile you need to pase a code they provide in the Class1.cs file which is the following:
using System;
using System.ServiceModel;
internal class MyServiceHost
{
internal static ServiceHost myServiceHost = null;
internal static void StartService()
{
//Consider putting the baseAddress in the configuration system
//and getting it here with AppSettings
Uri baseAddress = new Uri("http://localhost:8080/service1");
//Instantiate new ServiceHost
myServiceHost = new ServiceHost(typeof(WCFServiceLibraryExample.service1), baseAddress);
//Open myServiceHost
myServiceHost.Open();
}
internal static void StopService()
{
//Call StopService from your shutdown logic (i.e. dispose method)
if (myServiceHost.State != CommunicationState.Closed)
myServiceHost.Close();
}
}
If you notice this internal class has an instantiated object of the ServiceHost class set to null. A ServiceHost class according to MSDN “implements the host used by the Windows Communication Foundation (WCF) service model programming model”. This instantiation of the class (myServiceHost) will help us to start and stop our service. Notice that the open and close of the service happens inside the methods StartService and StopService. This methods will be extremely helpful when we will attempt to write a little console application that can start and stop our service.
4-At this point we need to add an Application Configuration File to our host project.
5-In this blank App.config file you need to pase a code they provide in the Class1.cs file which is the following:
<system.servicemodel>
<services>
<service name=”WCFServiceLibraryExample.service1″>
<endpoint contract=”WCFServiceLibraryExample.IService1″ binding=”wsHttpBinding”>
</endpoint>
</service>
</services>
</system.servicemodel>
The tag “system.servicemodel” you could think of it as the tag you use to start connecting web service. In the “services” tag you can specify a specific “service” which you need to give a name with the name attribute of the service tag. Inside the “service” tag you specify the “endpoint” and the contract attribute of the endpoint tag. The contract attribute (which is what the endpoint is going to communicate to the outside world about our service) will be the interface that I had in my example “WCF: Creating a WCF Service”.
6- After this you want to add the code that will host, start and stop the service. This is my code for this step:
using System;
using System.Collections.Generic;
using System.Text;
namespace WCFConsoleService
{
class Program
{
static void Main(string[] args)
{
MyServiceHost.StartService();
Console.WriteLine("To stop the service type 'e'");
string exit = Console.ReadLine();
while (!exit.Equals("e"))
{
Console.WriteLine("To stop the service type 'e'");
exit = Console.ReadLine();
}
MyServiceHost.StopService();
}
}
}
I basically start the service, and wait for the user to type the character ‘e’ to stop the service.
7- After this you need to add a Reference to System.ServiceModel.dll in your host project. This is required because in your codefile the namespace System.ServiceModel is used. To do this right click on your host project, select add reference,select “System.ServiceModel.dll” in the .NET tab.
8- Now you need to add a Reference from your Host project to your Service Library project. To do this right click on your host project, select add reference, and in the projects tab select your wcf service project.
9-Set the host project as the “StartUp” project for the solution.
After this step you are ready to host your WCF Service. You can press F5 in Visual Studio and you should see a console with the following message:

This means your wcf service is running and you can stop it by pressing ‘e’. If you go to http://localhost:8080/service1 you should see the following:

Congratulations, you are now hosting a WCF Service.
