如何在 1 个 Windows 服务中托管 2 个 WCF 服务? [英] How to host 2 WCF services in 1 Windows Service?

查看:21
本文介绍了如何在 1 个 Windows 服务中托管 2 个 WCF 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WCF application that has two Services that I am trying to host in a single Windows Service using net.tcp. I can run either of the services just fine, but as soon as I try to put them both in the Windows Service only the first one loads up. I have determined that the second services ctor is being called but the OnStart never fires. This tells me that WCF is finding something wrong with loading up that second service.

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

Using net.tcp I know I need to turn on port sharing and start the port sharing service on the server. This all seems to be working properly. I have tried putting the services on different tcp ports and still no success.

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

My service installer class looks like this:

 [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 });
      }   
}

而且这两个服务看起来非常相似

and both services looking very similar

 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 文章 并创建两个服务主机.但不是直接调用每个服务主机,您可以将其分解为任意数量的类,这些类定义了您要运行的每个服务:

Base your service on this MSDN article and create two service hosts. But instead of actually calling each service host directly, you can break it out to as many classes as you want which defines each service you want to run:

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服务宿主的主体中,调用不同的类:

In the body of the windows service host, call the different classes:

    // 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 服务.

Then you can add as many WCF services as you like to one windows service host.

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

REMEBER to call the stop methods as well....

这篇关于如何在 1 个 Windows 服务中托管 2 个 WCF 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆