Multiple services, in multiple processes, but a single assembly

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have defined multiple ServiceBase-derived services within a single
assembly. If I follow the MSDN examples and create a single project
installer, a single ServiceProcessInstaller, and multiple ServiceInstallers,
everything works as expected. However, all services are hosted in a single
process - which is not what I want to happen; I want each to run in its own
process.

I have tried creating individual project and process installers for each
service with the same problematic results. All services appear to install.
Each service started runs in its own process. However, independent of which
service is started, only the first service defined in the array
'ServicesToRun' passed to
System.ServiceProcess.ServiceBase.Run(ServicesToRun) will start. It will
start multiple times - once for each service defined, but it is the same
service in each case. If I change the order of the service elements in the
'ServicesToRun' array, I can change which service actually starts. The
ServiceName on the installers all match their respective services, so I
don't understand what is wrong.

Is it possible to host multiple services each in its own process and yet
have them all defined in a single assembly? I have toyed around with
command-line arguments to the service, but the installers don't appear to be
able to easily define command-line arguments for the host executable.

Any suggestions would be greatly appreciated.
Thanks,
Dolph
 
Hello Dolph,

Thanks for your post. As I understand, the problem you are facing is how to
have multiple services in a single assembly and excute them in different
processes. Please correct me if there is any misunderstanding. Now I'd like
to share the following information with you:

1. When a service is configured as "Win32ShareProcess", the SCM knows that
the process is going to create multiple services. So when you call
ServiceBase.Run() to register multiple services, it keeps track of each one
and can start/stop each one independantly. Note that all "shared" services
need to be configured the same so that it doesn't matter which one gets
started first.

When a service is configured as "Win32OwnProcess", then the SCM will call
CreateProcess() each time the service is started and only look at the first
service in the list of services registered via ServiceBase.Run(). This can
get tricky if multiple services point at the same binary. Since the SCM is
only going to use the first in the list, how will your process know which
service the SCM is trying to start? One way to work around the problem is
to modify the binPath so that each service has points to the same binary
but with different command line paramenters, then you can use the command
line to know which service class to create and register.

The difficult part of this is configuring the ervices. Unfortunately, it
doesn't appear that ServiceInstaller is capable of neither setting the
Share/Own flag for the service nor adding command line parameters. You
would need to interop out to the Win32 ChangeServiceConfig() to change the
configuration after the service has been installed.

2. In the meantime, please also consider the following cons and pros of
having multiple services in a single assmely. For example, a single binary
makes shipping easier and reduces system resource usage. However, it makes
debugging really difficult because all the services are in the same
process. Its hard to isolate problems. It also adds a lot of opportunity
for bugs related to interaction. Last, it makes it impossible to update the
binary of one service without stopping all the related services.

As you can see, the cons completely outweigh the pros, so I recommend you
NOT go this route.

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top