wcf - 您可以在单个 Windows 服务中托管多个 WCF 进程吗?

标签 wcf windows-services

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

最佳答案

编辑:所以似乎正在修剪我冗长的代码/配置示例,所以这里有一个完整的解释: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 });
    }
}

还有一些配置
  • 在这里,绑定(bind)和行为配置是跨服务共享的,但您可能需要针对不同类型的服务进行不同的配置。
  • 我为不同的服务使用不同的端口,但您不必这样做。




















    ...


















  • 关于wcf - 您可以在单个 Windows 服务中托管多个 WCF 进程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1308975/

    相关文章:

    c# - WCF 可以使用 https 访问 SOAP 1.1 服务吗?

    c# - 命名管道客户端无法连接到作为网络服务运行的服务器

    c# - 如何从 Windows 服务运行 excel vba 代码

    c# - Windows 服务上的 Unity DI,可能吗?

    c# - 带有 SSL 的 webHttpBinding 的 WCF 配置在负载均衡器处终止

    c++ - 如何将 C++ 程序连接到 WCF 服务?

    wcf - SSL 服务器证书存在 WCF 的一个名称?

    vb.net - 以前的线程完成后,如何启动新线程?

    c# - 托管在 Windows 服务中的 WCF 服务运行速度比控制台应用程序慢 10 倍

    本地运行时出现 WCF Azure 403 错误