【问题标题】:Can you host multiple WCF processes in a single windows service?您可以在单个 Windows 服务中托管多个 WCF 进程吗?
【发布时间】:2009-08-20 21:34:28
【问题描述】:

我有一个托管在 Windows 服务中的 WCF 进程。 我想知道我是否可以安全地拥有多个 WCF 进程,这些进程在同一个 Windows 服务中托管不同的事情。 我需要担心端口吗? 我正在使用 mex 端点

【问题讨论】:

  • 您在单个 Windows 服务中托管了多个 WCF 服务,而不是多个进程

标签: wcf windows-services


【解决方案1】:

编辑:SO 似乎正在修剪我冗长的代码/配置示例,所以这里有一个完整的解释:http://thegrenade.blogspot.com/2009/08/hosting-multiple-wcf-services-under.html

这里有一个例子可以帮助你前进:

class Program {
    static void Main() {
        if (Environment.UserInteractive) {
            ServiceManager serviceManager = new ServiceManager();
            serviceManager.OpenAll();
            Console.ReadKey();
            serviceManager.CloseAll();
        }
        else
            ServiceBase.Run(new WindowsService());
    }
}

public class WindowsService : ServiceBase
{
    public static string WindowsServiceName = "Windows Service Name";
    public static string WindowsServiceDescription = "Windows Service Description";
    public static string WindowsServiceUsername = @".\username";
    public static string WindowsServicePassword = "password";

    private readonly ServiceManager serviceManager = new ServiceManager();

    private readonly IContainer components = new Container();

    protected override void Dispose(bool disposing) {
        if (serviceManager != null) serviceManager.CloseAll();
        if (disposing && (components != null)) components.Dispose();
        base.Dispose(disposing);
    }

    public WindowsService() {
        ServiceName = WindowsServiceName;
        CanStop = true;
    }

    protected override void OnStart(string[] args) {
        base.OnStart(args);
        serviceManager.OpenAll();
    }

    protected override void OnStop() {
        serviceManager.CloseAll();
        base.OnStop();
    }
}

public class ServiceManager {
    readonly List<ServiceHost> serviceHosts = new List<ServiceHost>();

    public void OpenAll() {
        OpenHost<Service1>();
        OpenHost<Service2>();
        ...
    }

    public void CloseAll() {
        foreach (ServiceHost serviceHost in serviceHosts)
            serviceHost.Close();
    }

    private void OpenHost<T>() {
        Type type = typeof(T);
        ServiceHost serviceHost = new ServiceHost(type);
        serviceHost.Open();
        serviceHosts.Add(serviceHost);
    }
}

/// <remarks>
/// Enables application to be installed as a Windows Service by running InstallUtil
/// </remarks>
[RunInstaller(true)]
public class WcfServiceHostInstaller : Installer {
    public WcfServiceHostInstaller() {
        Installers.Add(new ServiceInstaller
                           {
                               StartType = ServiceStartMode.Automatic,
                               ServiceName = WindowsService.WindowsServiceName,
                               Description = WindowsService.WindowsServiceDescription
                           });
        Installers.Add(new ServiceProcessInstaller { Account = ServiceAccount.User, Username = WindowsService.WindowsServiceUsername, Password = WindowsService.WindowsServicePassword });
    }
}

还有一些配置

  • 在这里,绑定和行为配置是跨服务共享的,但您可能需要为不同类型的服务提供不同的配置。
  • 我为不同的服务使用不同的端口,但你不必这样做。

    ...

【讨论】:

    【解决方案2】:

    是的,你可以。我正在我的项目中做这件事,在我的 Windows 服务中托管三个独立的 WCF 服务。只需确保每个 WCF 端点,即地址/绑定/合同元组,都是唯一的。

    【讨论】:

      【解决方案3】:

      看看这个Run WCF ServiceHost with multiple contracts,它并不完全符合您的要求,但可能有一些用处。

      使用它加上 ServiceBehaviour 属性的 InstanceContextMode 属性和配置Service throttling 的能力,您应该能够得到您想要的。

      【讨论】:

        【解决方案4】:

        与@Matt 一样,我也在此链接的帮助下完成了这项工作。

        http://www.codeproject.com/KB/WCF/generic_wcf_host.aspx

        【讨论】:

          猜你喜欢
          • 2011-07-13
          • 1970-01-01
          • 1970-01-01
          • 2010-12-13
          • 2020-11-24
          • 2013-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多