We have discussed briefly about different available hosting options (Self Hosting, Windows Service, IIS, WAS etc) for WCF Service in separate post “WCF Interview Questions and Answers – Part 1“. In this WCF Tutorial, we are going to implement hosting WCF Service in a Windows Service. Hosting in Windows Service is suitable for long-running WCF Service where the lifetime of the service is controlled by operating system.
Remember, in previous WCF Tutorial, we have already implemented WCF Self Hosting in a Console Application with example using a step by step approach. In this WCF Service Tutorial, we will follow the same step by step approach to host WCF Service as:
- Creating a Class Library for WCF Service
- Adding a Windows Service
Create a Class Library i.e. StudentService:
In Visual Studio, create a new Class Library Project, name it as “StudentService” and press “OK” button.
Then, right click on project and Add a new “WCF Service” to this Class Library Project.
{
[OperationContract]
string GetStudentInfo(int studentId);
}
{
public string GetStudentInfo(int studentId)
{
string studentName = string.Empty;
switch (studentId)
{
case 1:
{
studentName = “Muhammad Ahmad”;
break;
}
case 2:
{
studentName = “Muhammad Hamza”;
break;
}
default:
{
studentName = “No student found”;
break;
}
}
return studentName;
}
}
- StudentService class library
- System.ServiceModel.
As we want to host our WCF Service when Windows Service get started. We will write WCF Service hosting code in OnStart() method of Windows Service (StudentHost.cs) and close the Service Host in OnStop() method accordingly.
{
public partial class StudentHost : ServiceBase
{
ServiceHost studentServiceHost = null;
{
InitializeComponent();
}
{
try
{
//Base Address for StudentService
Uri httpBaseAddress = new Uri(“http://localhost:4321/StudentService”);
studentServiceHost.AddServiceEndpoint(typeof(StudentService.IStudentService),
new WSHttpBinding(), “”);
ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = true;
studentServiceHost.Description.Behaviors.Add(serviceBehavior);
studentServiceHost.Open();
catch (Exception ex)
{
studentServiceHost = null;
}
}
{
if (studentServiceHost != null)
{
studentServiceHost.Close();
studentServiceHost = null;
}
}
}
}
Note: Dont’ forget using System.ServiceModel and System.ServiceModel.Description.
Also, code for Main() method in Program.cs will be as follows:
{
static class Program
{ static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new StudentHost()
};
ServiceBase.Run(ServicesToRun);
}
}
}
For our Windows Service to be installed, we need to add an installer class to our project. Right click on Windows Service project i.e. WinServiceStudentHost and choose Add->New Item -> Installer Class as follows:
Following is the code for Host Installer class:
{
[RunInstaller(true)]
public partial class StudentHostInstaller : System.Configuration.Install.Installer
{
public StudentHostInstaller()
{//Process
ServiceProcessInstaller process = new ServiceProcessInstaller();
process.Account = ServiceAccount.NetworkService; //Process
ServiceInstaller service = new ServiceInstaller(); service.ServiceName = “StudentHostWindowService”;
service.DisplayName = “StudentHostWindowService”;
service.Description = “Student WCF Service Hosted Successfully.”;
service.StartType = ServiceStartMode.Automatic;
Installers.Add(process);
Installers.Add(service);
}
}
}
Note: Don’t forget using System.ServiceProcess.
Finally, we need to build our project. We can install our Windows Service through Visual Studio Command Prompt using InstallUtil utility. We will point to WinServiceStudentHost.exe in InstallUtil command as shown in below diagram.
Our Windows Service is installed successfully and hosted our StudentService. Now, we can easily consume that WCF Service in a Client Application. For implementation on creating a proxy to consume a WCF Service, you can follow my previous WCF Tutorial on Calling a WCF Self Hosting Service.
Hopefully, this WCF Service Tutorial will help in practically implementing Hosting WCF Service in Windows Service.
Other Related Tutorials:
Top 10 Interview Questions and Answers Series:
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions