Jacob Pettersson

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.



Re: Visual C# General Running two services in same VSTS project

TaylorMichaelL

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





Re: Visual C# General Running two services in same VSTS project

Jacob Pettersson

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.....





Re: Visual C# General Running two services in same VSTS project

TaylorMichaelL

It sort of sounds like your installers are both installing the same service. Look at the properties for each of your service installers (not the service process installer) and verify that each points to a separate service. Also be sure that both use the project installer as their parent. Rerun the InstallUtil program and look at the output to be sure that both services are installed correctly. Do you have any dependencies set up on the services

Michael Taylor - 9/29/06





Re: Visual C# General Running two services in same VSTS project

Jacob Pettersson

TY...

Dependencies was set up wrong.





Re: Visual C# General Running two services in same VSTS project

hfrmobile

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) ;-)






Re: Visual C# General Running two services in same VSTS project

Kevin Durdle

I've experienced a slightly different problem, which I've documented here: http://forums.microsoft.com/msdn/ShowPost.aspx postid=1471051&siteid=1

It's directly related to having two services run from a single process. If anyone has the time to look at the well documented steps I've followed, it would be appreciated!

Thanks,

Kevin