WCF – Windows Service Hosting
”;
The operation of Windows service hosting is a simple one. Given below are the steps with requisite coding and screenshots that explain the process in an easy way.
Step 1 − Now let’s create a WCF service. Open Visual Studio 2008 and click New → Project and select Class Library from the template.
Step 2 − Add reference System.ServiceModel to the project. This is the core assembly used for creating the WCF service.
Step 3 − Next, we can create the ISimpleCalulator interface. Add the Service and Operation Contract attribute as shown below −
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace WindowsServiceHostedService{ [ServiceContract] public interfaceISimpleCalculator { [OperationContract] int Add(int num1, int num2); [OperationContract] int Subtract(int num1, int num2); [OperationContract] int Multiply(int num1, int num2); [OperationContract] double Divide(int num1, int num2); } }
Step 4 − Implement the ISimpleCalculator interface as shown below −
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsServiceHostedService { Class SimpleCalulator : ISimpleCalculator { Public int Add(int num1, int num2) { return num1 + num2; } Public int Subtract(int num1, int num2) { return num1 - num2; } Public int Multiply(int num1, int num2) { return num1 * num2; } Public double Divide(int num1, int num2) { if (num2 != 0) return num1 / num2; else return 0; } } }
Step 5 − Build the Project and get the dll. Now, we are ready with the WCF service. We are going to see how to host the WCF service in Windows service.
Note − In this project, it is mentioned that we are creating both Contract and Service (implementation) in the same project. However it is always a good practice if you have both in different projects.
Step 6 − Open Visual Studio 2008 and Click New → Project and select Windows Service.
Step 7 − Add ”WindowsServiceHostedService.dll” as reference to the project. This assembly is going to act as service.
Step 8 − The OnStart method of the service can be used to write the hosting code for WCF. We have to make sure that we are using only one service host object. OnStop method is used to close the Service Host. The following code shows how to host the WCF service in Windows service.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.ServiceModel; using System.ServiceModel.Description; namespace WCFHostedWindowsService { Partial class WCFHostedWindowsService : ServiceBase { ServiceHostm_Host; Public WCFHostedWindowsService() { InitializeComponent(); } Private void InitializeComponent() { thrownewNotImplementedException(); } protectedoverridevoidOnStart(string[] args) { if (m_Host != null) { m_Host.Close(); } //Create a URI to serve as the base address UrihttpUrl = newUri("http://localhost:8090/WindowsServiceHostedService/SimpleCalculator"); //Create ServiceHost m_Host = newServiceHost typeof(WindowsServiceHostedService.SimpleCalulator), httpUrl); //Add a service endpoint m_Host.AddServiceEndpoint (typeof(WindowsServiceHostedService.ISimpleCalculator), newWSHttpBinding(), ""); //Enable metadata exchange ServiceMetadataBehaviorsmb = newServiceMetadataBehavior(); smb.HttpGetEnabled = true; m_Host.Description.Behaviors.Add(smb); //Start the Service m_Host.Open(); } protectedoverridevoidOnStop() { if (m_Host != null) { m_Host.Close(); m_Host = null; } } staticvoid Main() { ServiceBase[] ServicesToRun; ServicesToRun = newServiceBase[] { newWCFHostedWindowsService(); } ServiceBase.Run(ServicesToRun); } } }
Step 9 − In order to install the service, we need to have the Installer class for the Windows service. So add a new Installer class to the project, which is inherited from the Installer class. Given below is the code that shows the Service name, StartUp type, etc. of the service.
using System; using System.Collections.Generic; using System.Text; using System.ServiceProcess; using System.Configuration.Install; using System.ComponentModel; using System.Configuration; namespace WCFHostedWindowsService { [RunInstaller(true)] Public class WinServiceInstaller : Installer { Private ServiceProcessInstaller process; Private ServiceInstaller service; Public WinServiceInstaller() { process = newServiceProcessInstaller(); process.Account = ServiceAccount.NetworkService; service = newServiceInstaller(); service.ServiceName = "WCFHostedWindowsService"; service.DisplayName = "WCFHostedWindowsService"; service.Description = "WCF Service Hosted"; service.StartType = ServiceStartMode.Automatic; Installers.Add(process); Installers.Add(service); } } }
Step 10 − Build the project to get the executable file WCFHostedWindowsService.exe. Next, we need to install the service using the Visual Studio Command Prompt. So open the command prompt by clicking Start→All Programs→Microsoft Visual Studio 2008→Visual Studio Tools→ Visual Studio Command Prompt. Using the install util utility application, you can install the service as shown below.
”;