Windows Service Hosting
标签:
WCF - Windows Service HostingThe 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.
在windows服务上托管wcf是一个简单的操作。
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 ISimpleCalculator 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.Threading.Tasks; using System.ServiceModel; namespace WCFHostedWindowsService { [ServiceContract] interface ISimpleCalculator { [OperationContract] int Sum(int number1, int number2); [OperationContract] int Subtract(int number1, int number2); [OperationContract] int Multiply(int number1, int number2); [OperationContract] double Divide(int number1, int number2); } }
Step-4: Implement the ISimpleCalculator interface as shown below:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WCFHostedWindowsService { class SimpleCalculator : ISimpleCalculator { public int Sum(int number1, int number2) { return number1 + number2; } public int Subtract(int number1, int number2) { return number1 - number2; } public int Multiply(int number1, int number2) { return number1 * number2; } public double Divide(int number1, int number2) { if (number2 != 0) { return number1 / number2; } 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.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceModel; using System.ServiceModel.Description; using System.ServiceProcess; using WindowsServiceHostedService; namespace WCFHostedWindowsService { class WCFHostedWindowsService : ServiceBase { ServiceHost serviceHost; public WCFHostedWindowsService() { InitializeComponent(); } protected override void OnStart(string[] args) { if (serviceHost != null) { serviceHost.Close(); } //Create a URI to serve as the base address Uri httpUrl = new Uri(":8090/WindowsServiceHostedService/SimpleCalculator"); //Create ServiceHost serviceHost = new ServiceHost(typeof(SimpleCalculator), httpUrl); //Add a service endpoint serviceHost.AddServiceEndpoint(typeof(ISimpleCalculator), new WSHttpBinding(), "");//ISimpleCalulator //ISimpleCalulator //Enable metadata exchange ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; serviceHost.Description.Behaviors.Add(smb); //Start the Service serviceHost.Open(); } protected override void OnStop() { if (serviceHost != null) { serviceHost.Close(); serviceHost = null; } } /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param>如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { components = new System.ComponentModel.Container(); this.ServiceName = "Service1"; } #endregion } }
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70062.html