Multiple service processes (System.ServiceProcess.ServiceBase) in one Windows Service

核能气质少年 提交于 2019-12-30 06:31:09

问题


I've got two service processes (derived from System.ServiceProcess.ServiceBase) MyService1 and MyService2.

I'm trying to run them both in the Main() of a Windows Service's Programm.cs.

static void Main()
    {
        ServiceBase[] servicesToRun = { new MyService1(), new MyService2() };
        ServiceBase.Run(servicesToRun);
    }

In the OnStart methods of both MyService1 and MyService2 I write to a log file so I can tell they are running.

The system builds fine and I can install the service.

But only MyService1 runs. MyService2 doesn't do a thing (i.e. no start-up log entry). When I change the order in the array:

ServiceBase[] servicesToRun = { new MyService2(), new MyService1() }

only MyService2 runs.

To try to get to the bottom of this, I'm using a little tool AndersonImes.ServiceProcess.ServicesLoader (https://windowsservicehelper.codeplex.com/) to get around the limitation that you cannot directly debug a windows service in Visual Studio. With this tool I can get both services MyService1 and MyService2 to start and run next to each other. But I still don't know why Windows is running only the first item in the ServiceBase[] servicesToRun array.

Any ideas?


回答1:


I finally found the answer here: http://www.bryancook.net/2008/04/running-multiple-net-services-within.html. A well hidden resource, thanks 'bryan'! Hopefully this helps the next developer to save time...

The explanation there around ServicesDependedOn isn't quite matching what I see in my project though. It's not about starting them but making sure they are started. Check out https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.servicesdependedon%28v=vs.110%29.aspx as well. I don't need this because they do not depend on each other.



来源:https://stackoverflow.com/questions/30181160/multiple-service-processes-system-serviceprocess-servicebase-in-one-windows-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!