Windows服务托管的操作很简单.下面给出了必要的编码和截图,以简单的方式解释过程.
第1步 : 现在让我们创建一个WCF服务.打开Visual Studio 2008,然后单击New → 从模板中选择并选择类库.
第2步 : 将引用System.ServiceModel添加到项目中.这是用于创建WCF服务的核心程序集.
步骤3 : 接下来,我们可以创建ISimpleCalulator接口.添加服务和操作合同属性,如下所示 :
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); } }
第4步 : 实现ISimpleCalculator接口,如下所示 :
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; } } }
第5步 : 构建项目并获取DLL.现在,我们已准备好使用WCF服务.我们将看到如何在Windows服务中托管WCF服务.
注意 : 在这个项目中,提到我们在同一个项目中创建契约和服务(实现).但是,如果您同时拥有不同的项目,这总是一个好习惯.
第6步 : 打开Visual Studio 2008并单击New → 项目并选择Windows服务.
第7步 : 添加"WindowsServiceHostedService.dll"作为项目的参考.这个程序集将作为服务.
第8步 : 该服务的OnStart方法可用于编写WCF的托管代码.我们必须确保我们只使用一个服务主机对象. OnStop方法用于关闭服务主机.以下代码显示如何在Windows服务中托管WCF服务.
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); } } }
第9步 : 为了安装该服务,我们需要具有Windows服务的Installer类.因此,向项目添加一个新的Installer类,该类继承自Installer类.下面给出的代码显示了服务的服务名称,StartUp类型等.
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); } } }
第10步 : 构建项目以获取可执行文件WCFHostedWindowsService.exe.接下来,我们需要使用Visual Studio命令提示符安装该服务.因此,单击Start → All Programs → Microsoft Visual Studio 2008 → Visual Studio Tools → 打开命令提示符. Visual Studio命令提示符.使用install util实用程序,您可以按如下所示安装该服务.