Wcf-windows Service Hosting
The operation of Windows service hosting is a simple one. Given below is the steps with requisite coding and screenshots, that explain, the process in a easy-to-do.
Hosting WCF on a Windows service is a simple operation.
Step-1: Now let's create a WCF service. Open Visual Studio 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 Isimplecalculator interface. Add the Service and operation contract attribute as shown below:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.ServiceModel;namespacewcfhostedwindowsservice{[ServiceContract]InterfaceIsimplecalculator {[OperationContract]intSum (intNumber1,intnumber2); [OperationContract]intSubtract (intNumber1,intnumber2); [OperationContract]intMultiply (intNumber1,intnumber2); [OperationContract]DoubleDivide (intNumber1,intnumber2); }}
Step-4: Implement The Isimplecalculator interface as shown below:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacewcfhostedwindowsservice{classSimplecalculator:isimplecalculator { Public intSum (intNumber1,intnumber2) { returnNumber1 +number2; } Public intSubtract (intNumber1,intnumber2) { returnNumber1-number2; } Public intMultiply (intNumber1,intnumber2) { returnNumber1 *number2; } Public DoubleDivide (intNumber1,intnumber2) { if(Number2! =0) { returnNumber1/number2; } Else { return 0; } } }}
Step-5: Build The Project and get the DLL. Now, we is ready with the WCF service. We is going to the host of the WCF service in Windows service.
Note: In this project, it was mentioned that we were creating both contract and Service (Implementation) in the SAM E project. However it was always a good practice if you had both in different projects.
Step-6: Open Visual Studio, and Click new→project and select Windows Service.
Step-7: Add ' WindowsServiceHostedService.dll ' as reference to the project. This Assembly are going to act as service.
Step-8: The OnStart method of the service can is used to write the hosting code for WCF. We have the make sure and we are using the 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.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.ServiceModel;usingSystem.ServiceModel.Description;usingsystem.serviceprocess;usingWindowsservicehostedservice;namespacewcfhostedwindowsservice{classwcfhostedwindowsservice:servicebase {ServiceHost ServiceHost; PublicWcfhostedwindowsservice () {InitializeComponent (); } protected Override voidOnStart (string[] args) { if(ServiceHost! =NULL) {servicehost.close (); } //Create a URI to serve as the base addressUri Httpurl =NewUri ("Http://localhost:8090/WindowsServiceHostedService/SimpleCalculator"); //Create ServiceHostServiceHost =NewServiceHost (typeof(SimpleCalculator), httpurl); //ADD a service endpointServicehost.addserviceendpoint (typeof(Isimplecalculator),NewWshttpbinding (),"");//Isimplecalulator//Isimplecalulator//Enable Metadata ExchangeServiceMetadataBehavior SMB =NewServiceMetadataBehavior (); Smb. Httpgetenabled=true; SERVICEHOST.DESCRIPTION.BEHAVIORS.ADD (SMB); //Start the ServiceServicehost.open (); } protected Override voidOnStop () {if(ServiceHost! =NULL) {servicehost.close (); ServiceHost=NULL; } } /// <summary> ///the required designer variables. /// </summary> PrivateSystem.ComponentModel.IContainer components =NULL; /// <summary> ///clean up all the resources that are in use. /// </summary> /// <param name= "disposing" >true if the managed resource should be disposed, otherwise false. </param> protected Override voidDispose (BOOLdisposing) { if(Disposing && (Components! =NULL) ) {components. Dispose (); } Base. Dispose (disposing); } #regionCode generated by the Component Designer/// <summary> ///The Designer supports the required method-do not///Use the Code Editor to modify the contents of this method. /// </summary> Private voidInitializeComponent () { components=NewSystem.ComponentModel.Container (); This. ServiceName ="Service1"; } #endregion }}
Step-9: In order to install the service, we need to has 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.
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.
Wcf-windows Service Hosting