wcf - 如何在 1 个 Windows 服务中托管 2 个 WCF 服务?

标签 wcf windows-services

我有一个 WCF 应用程序,它有两个服务,我试图使用 net.tcp 在单个 Windows 服务中托管这些服务。我可以很好地运行任何一个服务,但是一旦我尝试将它们都放在 Windows 服务中,只有第一个加载。我已经确定正在调用第二个服务构造函数,但 OnStart 永远不会触发。这告诉我 WCF 发现加载第二个服务有问题。

使用 net.tcp 我知道我需要打开端口共享并启动服务器上的端口共享服务。这一切似乎都在正常工作。我尝试将服务放在不同的 tcp 端口上,但仍然没有成功。

我的服务安装程序类如下所示:

 [RunInstaller(true)]
 public class ProjectInstaller : Installer
 {
      private ServiceProcessInstaller _process;
      private ServiceInstaller        _serviceAdmin;
      private ServiceInstaller        _servicePrint;

      public ProjectInstaller()
      {
            _process = new ServiceProcessInstaller();
            _process.Account = ServiceAccount.LocalSystem;

            _servicePrint = new ServiceInstaller();
            _servicePrint.ServiceName = "PrintingService";
            _servicePrint.StartType = ServiceStartMode.Automatic;

            _serviceAdmin = new ServiceInstaller();
            _serviceAdmin.ServiceName = "PrintingAdminService";
            _serviceAdmin.StartType = ServiceStartMode.Automatic;

            Installers.AddRange(new Installer[] { _process, _servicePrint, _serviceAdmin });
      }   
}

并且两种服务看起来非常相似
 class PrintService : ServiceBase
 {

      public ServiceHost _host = null;

      public PrintService()
      {
            ServiceName = "PCTSPrintingService";
            CanStop = true;
            AutoLog = true;

      }

      protected override void OnStart(string[] args)
      {
            if (_host != null) _host.Close();

            _host = new ServiceHost(typeof(Printing.ServiceImplementation.PrintingService));
            _host.Faulted += host_Faulted;

            _host.Open();
      }
}

最佳答案

以此为基础提供您的服务 MSDN article并创建两个服务主机。
但不是实际直接调用每个服务主机,您可以将其分解为任意数量的类,这些类定义了您想要运行的每个服务:

internal class MyWCFService1
{
    internal static System.ServiceModel.ServiceHost serviceHost = null;

    internal static void StartService()
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
        }

        // Instantiate new ServiceHost.
        serviceHost = new System.ServiceModel.ServiceHost(typeof(MyService1));
        // Open myServiceHost.
        serviceHost.Open();
    }

    internal static void StopService()
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
            serviceHost = null;
        }
    }
};

在windows服务主机的主体中,调用不同的类:
    // Start the Windows service.
    protected override void OnStart( string[] args )
    {
        // Call all the set up WCF services...
        MyWCFService1.StartService();
        //MyWCFService2.StartService();
        //MyWCFService3.StartService();


    }

然后,您可以向一台 Windows 服务主机添加任意数量的 WCF 服务。

记得也调用 stop 方法....

关于wcf - 如何在 1 个 Windows 服务中托管 2 个 WCF 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54419/

相关文章:

c# - 在 Windows 服务中使用 WCF 服务时出错

.net - 服务契约(Contract)的 PrincipalPermission 属性

wcf - 角色运行的计算机是否相同?

wcf - 使用 wcf webinvoke post 时出现方法不允许错误

command-line - 如何获取所有以常用单词开头的Windows服务名称?

c# - 从 Windows 服务创建交互式提升进程并显示给登录用户

c# - CreateProcessAsUser : Service gets "5: Access Denied" trying to access network share

asp.net - 从 ASP.NET 页面启动/停止 Window 服务

.net - WCF代理提交崩溃-终止进程

wcf - Azure服务总线中继偶尔发生FaultException