Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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?

MSIL
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;
    //...
}
Posted

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900