以Window服务作为wcf的寄主
2011-08-10 00:14
239 查看
Demo下载:http://files.cnblogs.com/zhuawang/Service.rar
本主题概述了创建由 Windows 服务承载的 Windows Communication Foundation (WCF) 服务所需的基本步骤。此方案可通过托管 Windows 服务承载选项启用,此选项是在没有消息激活的安全环境中在 Internet 信息服务 (IIS) 外部承载的、长时间运行的 WCF 服务。服务的生存期改由操作系统控制。此宿主选项在 Windows 的所有版本中都是可用的。
可以使用 Microsoft 管理控制台 (MMC) 中的 Microsoft.ManagementConsole.SnapIn 管理 Windows 服务,并且可以将其配置为在系统启动时自动启动。此承载选项包括注册承载 WCF 服务作为托管 Windows 服务的应用程序域,因此服务的进程生存期由 Windows 服务的服务控制管理器 (SCM) 来控制。
服务代码包括服务协定的服务实现、Windows 服务类和安装程序类。服务实现类 CalculatorService 是 WCF 服务。CalculatorWindowsService 是 Windows 服务。要符合 Windows 服务的要求,该类继承自 ServiceBase 并实现 OnStart 和 OnStop 方法。在 OnStart 中,将为 CalculatorService 类型创建 ServiceHost 并打开它。在 OnStop 中,停止并释放服务。主机还负责提供服务主机基址,该基址已在应用程序设置中进行设置。安装程序类继承自 Installer,允许程序通过 Installutil.exe 工具安装为 Windows 服务。
将 Program.cs 重命名为 Service.cs。
将命名空间更改为 Microsoft.ServiceModel.Samples。
添加对下列程序集的引用。
System.ServiceModel.dll
System.ServiceProcess.dll
System.Configuration.Install.dll
将下面的 using 语句添加到 Service.cs。
VB
C#
C++
F#
JScript
复制
定义
VB
C#
C++
F#
JScript
复制
在称为
VB
C#
C++
F#
JScript
复制
创建从 ServiceBase 类继承的称为
VB
C#
C++
F#
JScript
复制
通过创建并打开新 ServiceHost 实例重写 OnStart 方法,如下面的代码所示。
VB
C#
C++
F#
JScript
复制
通过关闭 ServiceHost 重写 OnStop 方法,如下面的代码所示。
VB
C#
C++
F#
JScript
复制
创建称为
VB
C#
C++
F#
JScript
复制
移除当您创建项目时生成的
将应用程序配置文件添加到项目中。将文件内容替换为下面的配置 XML。
XML
复制
在“解决方案资源管理器”中右击 App.config 文件,然后选择“属性”。在“复制到输出目录”下,选择“如果较新则复制”。
此示例显式指定配置文件中的终结点。如果您不希望向服务添加任何终结点,则运行时为您添加默认终结点。在此示例中,由于服务的 ServiceMetadataBehavior 设置为 true,因此服务还启用了发布元数据。有关默认终结点、绑定和行为的更多信息,请参见简化配置和WCF 服务的简化配置。
打开 Visual Studio 2010 命令提示,导航到项目目录。在命令提示符处键入
在命令提示符处键入
如果对服务进行更改,则必须首先停止并卸载服务。若要停止该服务,请在 SCM 中右击该服务,然后选择“停止”,或者在命令提示符下键入 net stop WCFWindowsServiceSample。请注意,如果停止 Windows 服务然后运行客户端,则在客户端尝试访问该服务时,会发生 EndpointNotFoundException 异常。若要卸载 Windows 服务,请在命令提示符下键入 installutil /u bin\service.exe。
VB
C#
C++
F#
JScript
复制
与“自承载”选项一样,Windows 服务宿主环境要求写入一些宿主代码作为应用程序的一部分。该服务作为控制台应用程序实现,且包含其自己的宿主代码。在其他宿主环境(如 Internet 信息服务 (IIS) 中的 Windows 进程激活服务 (WAS) 宿主)中,开发人员没有必要编写宿主代码。
本主题概述了创建由 Windows 服务承载的 Windows Communication Foundation (WCF) 服务所需的基本步骤。此方案可通过托管 Windows 服务承载选项启用,此选项是在没有消息激活的安全环境中在 Internet 信息服务 (IIS) 外部承载的、长时间运行的 WCF 服务。服务的生存期改由操作系统控制。此宿主选项在 Windows 的所有版本中都是可用的。
可以使用 Microsoft 管理控制台 (MMC) 中的 Microsoft.ManagementConsole.SnapIn 管理 Windows 服务,并且可以将其配置为在系统启动时自动启动。此承载选项包括注册承载 WCF 服务作为托管 Windows 服务的应用程序域,因此服务的进程生存期由 Windows 服务的服务控制管理器 (SCM) 来控制。
服务代码包括服务协定的服务实现、Windows 服务类和安装程序类。服务实现类 CalculatorService 是 WCF 服务。CalculatorWindowsService 是 Windows 服务。要符合 Windows 服务的要求,该类继承自 ServiceBase 并实现 OnStart 和 OnStop 方法。在 OnStart 中,将为 CalculatorService 类型创建 ServiceHost 并打开它。在 OnStop 中,停止并释放服务。主机还负责提供服务主机基址,该基址已在应用程序设置中进行设置。安装程序类继承自 Installer,允许程序通过 Installutil.exe 工具安装为 Windows 服务。
构造服务并提供宿主代码
创建称为“Service”的新 Visual Studio 控制台应用程序项目。将 Program.cs 重命名为 Service.cs。
将命名空间更改为 Microsoft.ServiceModel.Samples。
添加对下列程序集的引用。
System.ServiceModel.dll
System.ServiceProcess.dll
System.Configuration.Install.dll
将下面的 using 语句添加到 Service.cs。
VB
C#
C++
F#
JScript
复制
using System.ComponentModel; using System.ServiceModel; using System.ServiceProcess; using System.Configuration; using System.Configuration.Install;
定义
ICalculator服务协定,如下面的代码所示。
VB
C#
C++
F#
JScript
复制
// Define a service contract. [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")] public interface ICalculator { [OperationContract] double Add(double n1, double n2); [OperationContract] double Subtract(double n1, double n2); [OperationContract] double Multiply(double n1, double n2); [OperationContract] double Divide(double n1, double n2); }
在称为
CalculatorService的类中实现服务协定,如下面的代码所示。
VB
C#
C++
F#
JScript
复制
// Implement the ICalculator service contract in a service class. public class CalculatorService : ICalculator { // Implement the ICalculator methods. public double Add(double n1, double n2) { double result = n1 + n2; return result; } public double Subtract(double n1, double n2) { double result = n1 - n2; return result; } public double Multiply(double n1, double n2) { double result = n1 * n2; return result; } public double Divide(double n1, double n2) { double result = n1 / n2; return result; } }
创建从 ServiceBase 类继承的称为
CalculatorWindowsService的新类。添加称为
serviceHost的局部变量以引用 ServiceHost 实例。定义调用
ServiceBase.Run(new CalculatorWindowsService)的
Main方法
VB
C#
C++
F#
JScript
复制
public class CalculatorWindowsService : ServiceBase { public ServiceHost serviceHost = null; public CalculatorWindowsService() { // Name the Windows Service ServiceName = "WCFWindowsServiceSample"; } public static void Main() { ServiceBase.Run(new CalculatorWindowsService()); }
通过创建并打开新 ServiceHost 实例重写 OnStart 方法,如下面的代码所示。
VB
C#
C++
F#
JScript
复制
// Start the Windows service. protected override void OnStart(string[] args) { if (serviceHost != null) { serviceHost.Close(); } // Create a ServiceHost for the CalculatorService type and // provide the base address. serviceHost = new ServiceHost(typeof(CalculatorService)); // Open the ServiceHostBase to create listeners and start // listening for messages. serviceHost.Open(); }
通过关闭 ServiceHost 重写 OnStop 方法,如下面的代码所示。
VB
C#
C++
F#
JScript
复制
protected override void OnStop() { if (serviceHost != null) { serviceHost.Close(); serviceHost = null; } }
创建称为
ProjectInstaller的新类,该类继承自 Installer,且其 RunInstallerAttribute 设置为 true。这允许 Windows 服务由 Installutil.exe 工具安装。
VB
C#
C++
F#
JScript
复制
// Provide the ProjectInstaller class which allows // the service to be installed by the Installutil.exe tool [RunInstaller(true)] public class ProjectInstaller : Installer { private ServiceProcessInstaller process; private ServiceInstaller service; public ProjectInstaller() { process = new ServiceProcessInstaller(); process.Account = ServiceAccount.LocalSystem; service = new ServiceInstaller(); service.ServiceName = "WCFWindowsServiceSample"; Installers.Add(process); Installers.Add(service); } }
移除当您创建项目时生成的
Service类。
将应用程序配置文件添加到项目中。将文件内容替换为下面的配置 XML。
XML
复制
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <!-- This section is optional with the new configuration model introduced in .NET Framework 4. --> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/> </baseAddresses> </host> <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service --> <endpoint address="" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" /> <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="False"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
在“解决方案资源管理器”中右击 App.config 文件,然后选择“属性”。在“复制到输出目录”下,选择“如果较新则复制”。
此示例显式指定配置文件中的终结点。如果您不希望向服务添加任何终结点,则运行时为您添加默认终结点。在此示例中,由于服务的 ServiceMetadataBehavior 设置为 true,因此服务还启用了发布元数据。有关默认终结点、绑定和行为的更多信息,请参见简化配置和WCF 服务的简化配置。
安装并运行服务
生成解决方案以创建Service.exe可执行文件。
打开 Visual Studio 2010 命令提示,导航到项目目录。在命令提示符处键入
installutil bin\service.exe来安装 Windows 服务。
注意: |
---|
如果您不使用 Visual Studio 2010 命令提示,请确保 %WinDir%\Microsoft.NET\Framework\v4.0.<current version>目录位于系统路径中。 |
services.msc以访问服务控制管理器 (SCM)。Windows 服务应作为“WCFWindowsServiceSample”出现在服务中。只有在 Windows 服务正在运行的情况下,WCF 服务才能响应客户端。若要启动该服务,请在 SCM 中右击它,然后选择“启动”,或者在命令提示符下键入 net start WCFWindowsServiceSample。
如果对服务进行更改,则必须首先停止并卸载服务。若要停止该服务,请在 SCM 中右击该服务,然后选择“停止”,或者在命令提示符下键入 net stop WCFWindowsServiceSample。请注意,如果停止 Windows 服务然后运行客户端,则在客户端尝试访问该服务时,会发生 EndpointNotFoundException 异常。若要卸载 Windows 服务,请在命令提示符下键入 installutil /u bin\service.exe。
示例
下面是本主题使用的代码的完整列表。VB
C#
C++
F#
JScript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel; using System.ServiceModel; using System.ServiceProcess; using System.Configuration; using System.Configuration.Install;namespace Microsoft.ServiceModel.Samples
{
// Define a service contract. [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")] public interface ICalculator { [OperationContract] double Add(double n1, double n2); [OperationContract] double Subtract(double n1, double n2); [OperationContract] double Multiply(double n1, double n2); [OperationContract] double Divide(double n1, double n2); }// Implement the ICalculator service contract in a service class. public class CalculatorService : ICalculator { // Implement the ICalculator methods. public double Add(double n1, double n2) { double result = n1 + n2; return result; } public double Subtract(double n1, double n2) { double result = n1 - n2; return result; } public double Multiply(double n1, double n2) { double result = n1 * n2; return result; } public double Divide(double n1, double n2) { double result = n1 / n2; return result; } }public class CalculatorWindowsService : ServiceBase { public ServiceHost serviceHost = null; public CalculatorWindowsService() { // Name the Windows Service ServiceName = "WCFWindowsServiceSample"; } public static void Main() { ServiceBase.Run(new CalculatorWindowsService()); }// Start the Windows service. protected override void OnStart(string[] args) { if (serviceHost != null) { serviceHost.Close(); } // Create a ServiceHost for the CalculatorService type and // provide the base address. serviceHost = new ServiceHost(typeof(CalculatorService)); // Open the ServiceHostBase to create listeners and start // listening for messages. serviceHost.Open(); }protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
}
// Provide the ProjectInstaller class which allows
// the service to be installed by the Installutil.exe tool
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private ServiceProcessInstaller process;
private ServiceInstaller service;
public ProjectInstaller()
{
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
service.ServiceName = "WCFWindowsServiceSample";
Installers.Add(process);
Installers.Add(service);
}
}
}
与“自承载”选项一样,Windows 服务宿主环境要求写入一些宿主代码作为应用程序的一部分。该服务作为控制台应用程序实现,且包含其自己的宿主代码。在其他宿主环境(如 Internet 信息服务 (IIS) 中的 Windows 进程激活服务 (WAS) 宿主)中,开发人员没有必要编写宿主代码。
相关文章推荐
- 使用IIS作为宿主的WCF服务出现HTTP Error 404.3 - Not Found错误的解决办法
- 使用Winform程序作为WCF服务的宿主
- [WCF] 部署到iis, 无法找到 ServiceHost 指令中作为服务属性值提供的类型
- Silverlight访问Window服务为宿主的WCF跨域问题解决方案.
- WCF学习之一(2):控制台应用作为服务宿主(跨机器/网络)
- 使用网站作为WCF服务宿主的几种做法
- 在.net中使用wcf做服务时,千万不要使用127.0.0.1作为Endpoint的address
- 使用IIS作为宿主发布WCF服务常见错误以及解决方法
- WCF中 改名后 无法找到 ServiceHost 指令中作为服务属性值提供的类型“WCFDemo.Service1”。
- WCF:使用启用声明的 WCF Web 服务作为 SharePoint 2010 外部内容类型(第4 部分,共 4 部分)
- 在学习部署WCF如下:ServiceHost 指令中作为服务属性值提供的类型“Service.CalculatorService”。
- 使用Winform程序作为WCF服务的宿主
- Git服务搭建使用Linux作为服务端,window作为客户端
- WCF自定义集合类型作为服务契约注意事项
- WCF中 改名后 无法找到 ServiceHost 指令中作为服务属性值提供的类型“WCFDemo.Service1”。
- 使用网站作为WCF服务宿主的几种做法
- wcf服务和寄主的关系
- 使用SvrAny和InstSvr将应用程序作为Window服务启动
- 使用网站作为WCF服务宿主的几种做法
- WCF自定义集合类型作为服务契约注意事项