Running two services in same VSTS project
Hello...
I have created two c# windows services in the same VSTS project. They each have their own installers, and installs fine in the windows service list. I have attached a debugger to the main(), here is the code:
static void Main(string[] args)
{
ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new ServiceBase[] { new NetworkWatcher(), new LocalRunner() };
Debugger.Launch();
ServiceBase.Run(ServicesToRun);
}
Each service is set to manual start.
My problem is that when I start one of the services only NetworkWatcher is started, and LocalRunner is never entered. I can understand why the Main() is used for both services, but why isnt both services started?
I would prefer to be able to start each service independently, but if starting one service would cause both services to start - that would be fine too.
Any help appriciated.
You are confusing services with service processes. A service process (your main program in this case) can hold multiple services. However each service is installed and run separately. This allows one binary to hold multiple services. Windows uses this quite a bit. Most of the Windows services are housed in the same binary. However each must be started separately. Therefore you must start each of your services separately.
However if you want one service to start another then you can set a dependency on the service. For example the W3SVC is dependent upon the IISAdmin service. Therefore whenever you start W3SVC it will automatically start IISAdmin as well. This is handled automatically by Windows. You specify the dependencies during installation. The ServiceInstaller class has a property called ServicesDependedOn where you specify the dependencies.
For W3SVC it would look like this:
installer.ServicesDependedOn = new string[] { "IISAdmin" };
Note that the string(s) are the service names and not the display names. You can view this dependency information the Services administrator tool.
Michael Taylor - 9/28/06
Quote:
"My problem is that when I start one of the services only NetworkWatcher is started, and LocalRunner is never entered. I can understand why the Main() is used for both services, but why isnt both services started?"
I wasnt very clear here.... When I start the NetworkWatcher service, the debugger launches and a new instance NetworkWatcher is created just fine.... But when I try to start the LocalRunner service (Networkwatcher stopped) the same thing happens! - A new NetworkWatcher is created and Localrunner is only created when I stop the NetworkWatcher service manually.
Am I missing something.....?
Had the same issue today... (needed two services in one assembly)...
Comment by wizard:
Code Snippet
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
But this is not the whole truth. You also need a second installer otherwise only the first service will be visible and useable! No exception no warning, just ignores second service...
One ProcessInstaller and two ServiceInstaller!
My services have no dependencies (only the same base class) ;-)