it is possible to change the name of a service?

  • Thread starter Thread starter Andy Fish
  • Start date Start date
A

Andy Fish

Hi,

I am building a windows service using c#, using the ServiceInstaller and
ServiceProcessInstaller classes to install.

I would like to be able to have 2 separate installations of this service on
the machine (i.e. 2 copies of the same executable, 2 sets of registry
entries etc) so obviously one of them will need to have a different name.

I'm happy to do a bit of manual registry tweaking after installing the first
one, but I don't want to have to build 2 binaries with different service
names

I notice that both the ServiceBase and the ServiceInstaller set the
ServiceName to a hard-coded value. Is it possible to change the value at
runtime and how does this work with respect to registry entries etc?

TIA

Andy
 
Ok, FWIW here's my solution:

In the constructors for both the installer (i.e. the thing which inherits
from System.Configuration.Install.Installer) and the service itself (i.e.
the thing that inherits from System.ServiceProcess.ServiceBase), you can do
this:

this.ServiceName =
Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);

This makes the service name the same as the exe name, for 2 services, just
create 2 copies of the executable with different names and install them
using InstallUtil.

If you want to be a bit more sophisticated, you could use
GetExecutingAssembly() to figure out the directory of the current executable
and read a config file from there. AFAIK you'd need to read the config file
every time the service starts (not just on install) because there doesn't
seem to be any way of finding out the service name from inside a running
service.

Andy
 
and another footnote

My service was acting as a host for remoting. Unfortunately the remoting
framework would not stand the assembly being renamed, so I implemented the
config file solution after all. Maybe if you had your remoting objects
hosted in a different DLL you would still be able to rename the EXE and have
remoting working.
 
Back
Top