Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 224
EasierSaidThanDone Avatar asked May 12 '15 03:05

EasierSaidThanDone


1 Answers

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.

like image 155
EasierSaidThanDone Avatar answered Oct 12 '22 09:10

EasierSaidThanDone