在同一进程中的多个Windows服务中托管WCF服务

发布于 2024-10-09 18:48:23 字数 484 浏览 10 评论 0原文

我正在构建一个 Windows 服务来托管 WCF 服务,并且我正在使用:

var ServicesToRun = new ServiceBase[] 
            { new Service_1_Host() };
ServiceBase.Run(ServicesToRun);

我的问题是:

如果我有第二个服务及其主机,并且我在上面的代码中添加到数组中,如下所示:

var ServicesToRun = new ServiceBase[] { new Service_1_Host(), 
                                        new Service_2_Host() };
ServiceBase.Run(ServicesToRun);

第二个主机是否使用自己的应用程序域运行,或者我必须做一些配置才能使两个主机分别运行它单独的应用程序域?

I am building a windows service to host a WCF service, and I am using :

var ServicesToRun = new ServiceBase[] 
            { new Service_1_Host() };
ServiceBase.Run(ServicesToRun);

My question is:

if I have a second service with its host, and I added to the array in the above code, like the following:

var ServicesToRun = new ServiceBase[] { new Service_1_Host(), 
                                        new Service_2_Host() };
ServiceBase.Run(ServicesToRun);

does the second host run with its own app domain, or there is some configuration I have to do to make the two hosts run it separate app domain each?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唔猫 2024-10-16 18:48:23

您的做法完全错误......您需要区分:

  • Windows NT 服务(派生自 ServiceBase),它只是全天候运行

  • 实际的 WCF 服务主机(派生自 ServiceHost),提供真正的 WCF 服务接口。

基本上,您需要做的是:

  • 在您的 NT 服务 (ServiceBase) 中,有一个 OnStart 事件 - 在该事件中,您需要创建并打开您的WCF ServiceHost 实例 - NT 服务 (ServiceBase) 中的每个 WCF 服务(实现)类

  • 在该 NT 服务 (ServiceBase) 中,有一个 OnStop 事件,您应该在其中关闭 WCF 服务主机

有些您的代码看起来大致类似于:

using System;
using System.ServiceModel;

namespace YourNameSpace
{
    public class WcfHostService : ServiceBase
    {
        private ServiceHost _serviceHost1 = null;
        private ServiceHost _serviceHost2 = null;

        protected override void OnStart(string[] args)
        {
            // instantiate new ServiceHost instances 
            if (_serviceHost1 == null)
            {
                _serviceHost1 = new ServiceHost(typeof(YourService1));
            }

            if (_serviceHost2 == null)
            {
                _serviceHost2 = new ServiceHost(typeof(YourService2));
            }

            // open service hosts
            _serviceHost1.Open();
            _serviceHost2.Open();
        }

        protected override void OnStop()
        {
            if (_serviceHost1.State != CommunicationState.Closed)
            {
                _serviceHost1.Close();
            }

            if (_serviceHost2.State != CommunicationState.Closed)
            {
                _serviceHost2.Close();
            }
        }
    }
}

然后在您的 NT 服务主应用程序中,您将拥有:

var servicesToRun = new ServiceBase[] { new WcfHostService() };
ServiceBase.Run(servicesToRun);

就这样,真的!

You approaching this all wrong.... you need to keep apart:

  • the Windows NT Service (derived from ServiceBase) which is merely here to be running around the clock

  • the actual WCF service hosts (derived from ServiceHost) which provide the real WCF service interfaces.

Basically, what you need to do is this:

  • in your NT Service (ServiceBase) there's an OnStart event - inside that event, you need to create and open your WCF ServiceHost instances - one per WCF service (implementation) class

  • in that NT Service (ServiceBase) there's an OnStop event, inside of which you should close your WCF service hosts

Some your code would look roughly something like:

using System;
using System.ServiceModel;

namespace YourNameSpace
{
    public class WcfHostService : ServiceBase
    {
        private ServiceHost _serviceHost1 = null;
        private ServiceHost _serviceHost2 = null;

        protected override void OnStart(string[] args)
        {
            // instantiate new ServiceHost instances 
            if (_serviceHost1 == null)
            {
                _serviceHost1 = new ServiceHost(typeof(YourService1));
            }

            if (_serviceHost2 == null)
            {
                _serviceHost2 = new ServiceHost(typeof(YourService2));
            }

            // open service hosts
            _serviceHost1.Open();
            _serviceHost2.Open();
        }

        protected override void OnStop()
        {
            if (_serviceHost1.State != CommunicationState.Closed)
            {
                _serviceHost1.Close();
            }

            if (_serviceHost2.State != CommunicationState.Closed)
            {
                _serviceHost2.Close();
            }
        }
    }
}

and then in your NT Service main app, you would have:

var servicesToRun = new ServiceBase[] { new WcfHostService() };
ServiceBase.Run(servicesToRun);

That's all, really!

夏尔 2024-10-16 18:48:23

请注意,这是一个 ServiceBase 数组,而不是 HostsBase 之类的数组。您只需启动多个 Windows 服务即可。这与 WCF 甚至与 AppDomains 无关。

Notice that this is an array of ServiceBase, not of HostsBase or something. You are simply starting up multiple Windows Services. This has nothing to do with WCF or even with AppDomains.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文