In order to host a Windows Communication Foundation Service, we normally need a managed process, a ServiceHost instance and an endpoint configured forWCF Service.We can host aWCF Service in following different possible ways:
- Hosting in a Managed Application/ Self Hosting
- Console Application
- Windows/WPF Application
- Windows Service
- Hosting on Web Server
- IIS 6.0 (ASP.NET Application supports only HTTP)
- Windows Process Activation Service (WAS) i.e. IIS 7.0 supports HTTP, TCP, NamedPipes, MSMQ.
In this WCF Tutorial, focus is to Self Host our WCF Service in a Console Application using step by step approach. Self Hosting a WCF Service in a console application is comparatively easy as well as flexible because we can achieve the purpose by writing few lines of code.
Let’s first Create a Simple WCF Service i.e. a StudentService and then host in a Console application. StudentService having service operation GetStudentInfo that takes StudentId as parameter and returns student name.
1. Create StudentService Class Library:
Open Visual Studio and Create a new Class Library Project, name it as “StudentService” and press “OK” button.
{
[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.
httpBaseAddress);
Now, simply build the console application and run it after setting as startup project. You will see the following screen that shows our self-hosted WCF Service is running.
In this WCF Service Tutorial, we have created a Windows Communication Foundation Service and hosted in a Console Application. In following post on this WCF blog, we will call this self hosted WCF service from a client application.
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