it is possible to change the name of a service?

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
 
A

Andy Fish

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
 
A

Andy Fish

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top