开发者

Stopping a service from another service

I have one service that simply is for starting and stopping another service I created. The problem I am having is when I try to stop the other service named TUT from the Controller service named TUTCo开发者_运维技巧ntroller It stops itself instead of the intended service.

  public TUTController()
    {
        InitializeComponent();
        observer = new StatusObserver();
        observer.Update += new EventHandler<CustomArgs>(observer_Update);
    }

    void observer_Update(object sender, CustomArgs e)
    {
        ServiceController sc = new ServiceController("TUT");
        sc.Refresh();

        switch (e.CurrentStatus)
        {
            case StatusObserver.Status.On:
                if (sc.Status == ServiceControllerStatus.Stopped)
                {
                    sc.Start();
                    sc.Refresh();
                }
                break;
            case StatusObserver.Status.Off:
                if (sc.Status == ServiceControllerStatus.Running)
                {
                    sc.Stop();
                    sc.Refresh();
                }
                break;
            default:
                break;
        }
    }

The observer is a class that simply check the database so often for a Off/On value and publishes it back via the Update event. Thanks in advance.


I would suggest checking the names and dependencies of the two services.

Are you sure TUT & TUTController are the names of the services you think?

More so, is it possible that the SCM has a TUT listed as a dependency of TUTController (rather than the other way around) and so causing it to get shut down?


You can use messaging.

For Instance, there are TUTService and SomeService. When some service want to stop TUTService, SomeService send message StartServiceMessage to TuTService In TUTService you implement MessageHandler to process StartServiceMessage sent by SomeService.

in MessageHandler logic, you can have sc.stop()

You can use NServiceBus to implement the messaging

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜