在托管 Windows 服务中承载 WCF 服务

简介:

本主题概述了创建由 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 服务。

构造服务并提供宿主代码

  1. 创建称为“Service”的新 Visual Studio 控制台应用程序项目。

  2. 将 Program.cs 重命名为 Service.cs。

  3. 将命名空间更改为 Microsoft.ServiceModel.Samples。

  4. 添加对下列程序集的引用。

    • System.ServiceModel.dll
       
    • System.ServiceProcess.dll
       
    • System.Configuration.Install.dll
       
  5. 将下面的 using 语句添加到 Service.cs。

    C#
    VB
     
    using System.ComponentModel;
    using System.ServiceModel;
    using System.ServiceProcess;
    using System.Configuration;
    using System.Configuration.Install;
    
    
  6. 定义 ICalculator 服务协定,如下面的代码所示。

    C#
    VB
     
    // 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);
    }
    
    
  7. 在称为 CalculatorService 的类中实现服务协定,如下面的代码所示。

    C#
    VB
     
    // 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;
        }
    }
    
    
  8. 创建从 ServiceBase 类继承的称为 CalculatorWindowsService 的新类。添加称为 serviceHost 的局部变量以引用 ServiceHost 实例。定义调用 ServiceBase.Run(new CalculatorWindowsService) 的 Main方法

    C#
    VB
     
    public class CalculatorWindowsService : ServiceBase
    {
        public ServiceHost serviceHost = null;
        public CalculatorWindowsService()
        {
            // Name the Windows Service
            ServiceName = "WCFWindowsServiceSample";
        }
    
        public static void Main()
        {
            ServiceBase.Run(new CalculatorWindowsService());
        }
    
    
  9. 通过创建并打开新 ServiceHost 实例重写 OnStart 方法,如下面的代码所示。

    C#
    VB
     
    // 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();
    }
    
    
  10. 通过关闭 ServiceHost 重写 OnStop 方法,如下面的代码所示。

    C#
    VB
     
    protected override void OnStop()
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
            serviceHost = null;
        }
    }
    
    
  11. 创建称为 ProjectInstaller 的新类,该类继承自 Installer,且其 RunInstallerAttribute 设置为 true。这允许 Windows 服务由 Installutil.exe 工具安装。

    C#
    VB
     
    // 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);
        }
    }
    
    
  12. 移除当您创建项目时生成的 Service 类。

  13. 将应用程序配置文件添加到项目中。将文件内容替换为下面的配置 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 服务的简化配置

安装并运行服务

  1. 生成解决方案以创建 Service.exe 可执行文件。

  2. 打开 Visual Studio 2010 命令提示,导航到项目目录。在命令提示符处键入 installutil bin\service.exe 来安装 Windows 服务。

    ms733069.note(zh-cn,VS.100).gif注意:
    如果您不使用 Visual Studio 2010 命令提示,请确保 %WinDir%\Microsoft.NET\Framework\v4.0.<current version> 目录位于系统路径中。

     

     

    在命令提示符处键入 services.msc 以访问服务控制管理器 (SCM)。Windows 服务应作为“WCFWindowsServiceSample”出现在服务中。只有在 Windows 服务正在运行的情况下,WCF 服务才能响应客户端。若要启动该服务,请在 SCM 中右击它,然后选择“启动”,或者在命令提示符下键入 net start WCFWindowsServiceSample

  3. 如果对服务进行更改,则必须首先停止并卸载服务。若要停止该服务,请在 SCM 中右击该服务,然后选择“停止”,或者在命令提示符下键入 net stop WCFWindowsServiceSample。请注意,如果停止 Windows 服务然后运行客户端,则在客户端尝试访问该服务时,会发生 EndpointNotFoundException 异常。若要卸载 Windows 服务,请在命令提示符下键入 installutil /u bin\service.exe

示例

下面是本主题使用的代码的完整列表。

C#
VB
 
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) 宿主)中,开发人员没有必要编写宿主代码。

另请参见










本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/1181029,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
1月前
|
存储 安全 数据安全/隐私保护
Windows部署WebDAV服务并映射到本地盘符实现公网访问本地存储文件
Windows部署WebDAV服务并映射到本地盘符实现公网访问本地存储文件
269 0
|
2月前
|
Java Unix 应用服务中间件
使用java service wrapper把windows flume做成服务
使用java service wrapper把windows flume做成服务
|
2月前
|
Windows
修改Windows服务的配置
修改Windows服务的配置
|
3月前
|
Arthas 监控 Java
Arthas 可以用于监控和诊断在 Windows 系统下部署的 Tomcat 服务
Arthas 可以用于监控和诊断在 Windows 系统下部署的 Tomcat 服务
175 2
|
26天前
|
Shell Windows
Windows服务器 开机自启动服务
Windows服务器 开机自启动服务
14 0
|
16天前
|
Kubernetes Linux Windows
kubectl 本地远程链接k8s多个集群,远程管控多集群,查看日志 部署服务(windows版)
kubectl 本地远程链接k8s多个集群,远程管控多集群,查看日志 部署服务(windows版)
226 0
|
4月前
|
网络协议 安全 文件存储
Windows本地搭建WebDAV服务并使用内网穿透远程访问【无公网IP】
Windows本地搭建WebDAV服务并使用内网穿透远程访问【无公网IP】
|
4月前
|
监控 Linux 定位技术
Linux【环境部署 01】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)
Linux【环境部署 01】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)
662 0
|
4天前
|
网络协议 安全 测试技术
Windows安装禅道系统结合Cpolar实现公网访问内网BUG管理服务
Windows安装禅道系统结合Cpolar实现公网访问内网BUG管理服务