Windows Service托管
在这篇教程中我们将看到如何将WCF服务托管到Windows服务中去。和将服务托管在非message activated的IIS中一样,我们使用相同的代码将WCF服务托管的代码写在控制台应用程序中。将服务托管在Windows服务中以下好处。
1.当系统启动的时候,服务就会跟着启动
2.WCF进程生命周期可以被Service Control Manager控制
3.所有的Windows版本都支持托管
Step 1: 现在让我们开始创建一个WCF服务。打开VS2008,点击新建->工程,从模板中选择类库。
Step 2: 工程参照中添加System.ServiceModel引用,这是创建WCF服务的核心DLL
Step 3: 如下创建一个ISimpleCalulator的接口。添加ServiceContract和OperationContract
ISimpleCalculator.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace WindowsServiceHostedContract { [ServiceContract] public interface ISimpleCalculator { [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: 添加ISimpleCalculator的实现代码
SimpleCalulator.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsServiceHostedService { class SimpleCalculator : 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: 编译工程得到DLL,现在我们已经准备好了WCF服务,我们再来看看如何将WCF服务托管在Windows服务中。注意: 在这个工程中,我们在相同的工程中创建了契约接口和实现类。将他们放在不同的工程中才是好的实践。
Step 6: 打开VS2008,点击新建->工程,选择Windows Service
Step 7: 想工程中添加WindowsServiceHostedService.dll参照,这个DLL将会像服务一样工作。
Step 8: 在OnStart方法中我们可以编写托管WCF的代码。我们必须保证我们只用一个Service Host对象。OnStop方法中我们需要关闭Service Host。参照下面的代码,我们可以将WCF服务托管在Windows服务中。
WCFHostedWindowsService.cs
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 { ServiceHost m_Host; public WCFHostedWindowsService() { InitializeComponent(); } protected override void OnStart(string[] args) { if (m_Host != null) { m_Host.Close(); } //Create a URI to serve as the base address Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator"); //Create ServiceHost m_Host = new ServiceHost (typeof(WindowsServiceHostedService.SimpleCalculator), httpUrl); //Add a service endpoint m_Host.AddServiceEndpoint (typeof(WindowsServiceHostedService.ISimpleCalculator), new WSHttpBinding(), ""); //Enable metadata exchange ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; m_Host.Description.Behaviors.Add(smb); //Start the Service m_Host.Open(); } protected override void OnStop() { if (m_Host != null) { m_Host.Close(); m_Host = null; } } static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new WCFHostedWindowsService() }; ServiceBase.Run(ServicesToRun); } } }
Step 9: 添加Installer类以便安装服务。所以想工程中添加Installer类,这个类继承Installer。在以下代码中查找服务名称,StartUp类
ServiceInstaller.cs
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 = new ServiceProcessInstaller(); process.Account = ServiceAccount.NetworkService; service = new ServiceInstaller(); service.ServiceName = "WCFHostedWindowsService"; service.DisplayName = "WCFHostedWindowsService"; service.Description = "WCF Service Hosted"; service.StartType = ServiceStartMode.Automatic; Installers.Add(process); Installers.Add(service); } } }
Step 10: 编译工程,我们得到程序WCFHostedWindowsService.exe。接下来我们用VS命令行工具来安装服务。开始菜单->所有程序->Microsoft Visual Studio 2008->Visual Studio Tools-> Visual Studio Command Prompt,使用InstallUtil.exe工具来安装服务。
Step 11:现在服务已经被成功托管,我们可以创建代理类并且可以开始使用客户端程序了。