Introduction
Here in this blog, we are going to discuss step by step how to host WCF in windows service. Here we will create a WCF self-hosted windows service that will host WCF service at run time. Let’s go to the getting started section for more details.
Getting Started
The following steps help to create WCF self-hosted windows service where the WCF service will be hosted at runtime, follow the steps.
Steps to host WCF in windows service
Open your source code where you have written code for the WCF service.
Then add a new windows service to your project solution, the below steps adds a widow service.
Right-click on your project solution.
Then go to “Add” and click on “New Project”.
The “Add new project” will appear, in the search box search for “Windows Service” project template.
Select the “Windows Service (.Net Framework)” project template, give a name and click on the “OK” button.
On your windows service take reference to your WCF service.
Open the service as code view by right-clicking on the service(service1.cs file) file and on “View Code”.
Inside the OnStart method write the hosting code, see the hosting code example in the below of these steps.
Now you are done with hosting WCF service in windows service.
Build your windows service application in realease mode and prepare for installation
See the related artcile secetion in the below, the list of blog links has been given that guides how to install windows service
WCF self hosting example
// Create a URI to serve as the base address
//
//Address pattern protocol: hostname: port / Namespace / classname
//
Uri Url = new Uri("http://localhost:8090/HellowWorldService/HelloWorld");
//Create ServiceHost
ServiceHost host = new ServiceHost(typeof(HellowWorldService.IHelloWorldr), httpUrl);
//Add a service endpoint
host.AddServiceEndpoint(typeof(HellowWorldService.IHelloWorld), newWSHttpBinding(), "");
//Enable metadata exchange
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(behavior);
//Start the Service
host.Open();
WCF self hosting example
In the above code example, the first line code is to create an object of Uri class to get the base address of your service, this class takes a string argument in his constructor. The string argument is the address pattern of your service like the following.
Address pattern: protocol:hostname:port/Namespace/classname
The second line of code creates an object of service host class that creates an environment for hosting WCF service. The remaining codes are is for setting the behavior of service.
To host your service you have to implement or use the following main 3 classes.
- Uri:
This class is used to create a base address (URL) for your WCF service, Its comes under the system namespace. fore more details Click Here
- ServiceHost:
Service host class is used to create or provides a host for your service and this class comes under System.ServiceModel namespace. Click here for more details
- ServiceMetadataBehavior:
This class comes under System.ServiceModel.Description namespace and System.ServiceModel (in System.ServiceModel.dll) assembly. For more details click here.
Related Articles
Summary
In the above of this blog, we saw how to host wcf service in windows service. I hope you have enjoyed it a lot.
Thanks