我应该将计时器放置在NT服务还是单例WCF服务中? [英] Should I place timer in NT-Service or singleton WCF-service?

查看:61
本文介绍了我应该将计时器放置在NT服务还是单例WCF服务中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NT服务(托管Windows服务),该服务通过ServiceHost托管单个 WCF服务.通过WCF接口,我从GUI更新了一些设置.今天,我在托管的WCF服务中有一个计时器 ,该计时器每3分钟启动一个新的后台工作程序,并进行一些繁重的工作.我的问题是:可以在WCF服务中使用计时器吗?还是将其移至NT服务更合乎逻辑?

我想确定如果繁重的工作"部分出现问题,则NT服务会停止.订阅_serviceHost.Faulted + = ServiceHostFaulted是否足够;我在哪里叫Stop()?这样是否会每次 停止nt服务?在WCF服务中出现问题了吗?

I have a NT-Service (a managed windows service) hosting a singleton WCF-Service with ServiceHost. Trough a WCF-interface I update some settings from a GUI. Today I have a timer in the hosted WCF-service that starts a new backgroundworker every 3 minutes and does some heavy work. My question is: is it ok to have the timer in the WCF-service? Or is it more logical to move it to the NT-service?

I want to be sure that if something goes wrong in the "heavy work"-section the NT-service stops. Is it enough to subscribe to the _serviceHost.Faulted += ServiceHostFaulted; where I call Stop()? Will this stop the nt-service every time something goes wrong in the WCF-service?

public class MyManagedWindowsService : ServiceBase
{
    private ServiceHost _serviceHost;
 
    protected override void OnStart(string[] args)
    {
        if (_serviceHost != null)
            _serviceHost.Close();
        _serviceHost = new ServiceHost(new MyServiceStub());
        _serviceHost.Faulted += ServiceHostFaulted;
        _serviceHost.Open();
        ((IMyService)_serviceHost.SingletonInstance).Start();
    }
    void ServiceHostFaulted(object sender, EventArgs e)
    {
        //When something goes really bad in wcf-service we end up here every time?
        Stop(); //ServiceBase.Stop()
    }
    protected override void OnStop()
    {
        if (_serviceHost != null)
        {
            try
            {
                _serviceHost.Close();
                _serviceHost = null;
            }
            catch (Exception exception)
            {
                Log.Error(exception);
            }
        }
    }
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]
public class MyServiceStub : IMyService
{
    private Timer _timer;
    private BackgroundWorker _worker;
    //...
}

推荐答案

对我来说,在Windows服务而不是WCF服务中设置计时器会更加合乎逻辑.您创建WCF服务以将服务"公开给外界. WCF服务的职责是交付服务,而Windows服务的职责是托管WCF服务并启动您所说的繁重工作部分.
To me, it would be more logical to have the timer in the windows service instead of the WCF service. You create a WCF service for exposing a "service" to the outside world. The WCF service''s responsibility is for delivering the service and the windows services responsibility is hosting the WCF service and starting the heavy work section as you called it.


这篇关于我应该将计时器放置在NT服务还是单例WCF服务中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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