Every WCF service has to host in a Windows process called the host process. A single host process can have multiple WCF services and you can host same WCF service in multiple host processes.
There are different ways to host WCF service. Those are using IIS web server, Self-Hosting and WAS hosting. Each hosting type has its own unique features.
Here we discuss about Self Hosting of WCF Service. The main advantage of self hosting is you can host the WCF service without IIS web server.
WCF Service Self Hosting:
Self hosting is a technique in which the developer is responsible for developing and managing the life cycle of host process. We can host WCF service by using any .Net application like windows application, console application and window service. But before client access the WCF service this host application has to run.
Here we host the simple WCF service by using window service. For that, first create simple WCF service which writes user input to the file on C drive.
To create WCF service application open Visual Studio => Create Project => Select “WCF Service Application” under Windows template and give name as SimpleWcfService.
Design WCF Service ServiceContract as shown below.
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
public interface IService1
void SaveUserInput(string input);
Implement service contract of WCF service as shown below.
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
public class Service1 : IService1
public void SaveUserInput(string userInput)
StreamWriter file = new StreamWriter("C:\\WCFFile.txt", true);
Here SaveUserInput method() stores the user input to the text file WCFFile.txt on C drive.
Now develop the window service to host this WCF service. To create windows service open Visual Studio => Create Project=> Select “Windows Service” under Windows template and give name as WCFServiceSelfHosting.
Add reference to the SimpleWcfService.dll(WCF service, just before we created) and System.ServiceModel.dll for the Windows Service application. Implement windows service to host the WCF service as shown below.
public partial class Service1 : ServiceBase
protected override void OnStart(string args)
Uri baseAddress = new Uri("http://localhost:8080/SimpleWCFService");
host = new ServiceHost(typeof(SimpleWcfService.Service1), baseAddress);
protected override void OnStop()
To host the any WCF service, create object for ServiceHost class and pass your WCF service reference. Here we created the object for ServiceHost class and provided the reference of SimpleWcfService and base address as http://localhost:8080/SimpleWCFService to host the WCF service.
Add end point information of WCF services to the hosting application. Add App.Config file to the windows service and mention End point information as shown below.
Still now you created the windows service to host the WCF service application. For that you have to install the windows service. Add Installer to Service1.cs[Design] as shown below.
Project Installer is added to windows service application. Right Click on serviceInstaller1, select Properties and change DisplayName, ServiceName values to SimpleWCFService as shown below.
Right Click on Select serviceProcessInstaller1, select Properties and change Account value to LocalSystem as shown below.
Build the Window Service Application, .exe generated. Now you have to install this windows service on your system using installutil tool. Execute the below command on Visual studio command Tool.
installutil -i "D:\WCFServiceSelfHosting\WCFServiceSelfHosting\bin\Debug\WCFServiceSelfHosting.exe"
The output is as shown below.
Your windows service to host the WCF service is installed successfully and start the this window service. To start the window service go to Run=>enter services.msc. Start the SimpleWCFService as shown below.
Now try to browse the URL http://localhost:8080/SimpleWCFService which is mentioned in windows service while hosting your WCF service. You will see output as shown below.
Stop the windows service SimpleWCFService and try to browse http://localhost:8080/SimpleWCFService, you will get error.
In my next article, I will explain how to consume the WCF service.
SimpleWcfService.zip (17.58 kb)
WCFServiceSelfHosting.zip (39.60 kb)