8

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?

EasierSaidThanDone
  • 1,877
  • 4
  • 20
  • 29
  • What happens when you do `ServiceBase.Run(new MyService1());` followed by `ServiceBase.Run(new MyService2());`? Also, do the two have any type of dependency on each other or communication with each other? – DWright May 12 '15 at 03:40
  • @Dwight No dependency. Currently they don't do anything other than logging to a (their own) file. I tried running both separately. Same result. Only one (the first) of them runs. – EasierSaidThanDone May 12 '15 at 03:47
  • Maybe this helps: http://stackoverflow.com/questions/1688275/can-i-have-multiple-services-hosted-in-a-single-windows-executable – DWright May 12 '15 at 03:56

1 Answers1

10

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.

EasierSaidThanDone
  • 1,877
  • 4
  • 20
  • 29