Bodo said:
I'm trying to build my own class that inherits from
System.Configuration.Install.Installer to build my own functionality of
installing a windows service.
However I'm struggeling with the install method:
public virtual void Install( IDictionary stateSaver);
I can't find any clues on how to specify stateSaver when calling
Install(stateSaver) method.
There's no official documentation on this that I'm aware of, but I do happen
to know how this works. In any case, this part is very simple: "stateSaver"
can simply be empty initially.
Also I' missing any class properties to state the path and name of the
application to run as a service.
Has anyone managed successfully to build his own custom class that
registeres a windows service with c# using .net framework?
*raises hand*
In my case, it was a requirement that the installation parameters (service
name, display name etc.) be part of the configuration file, so a single
service could be installed multiple times under different names. (I don't
recommend this setup in general, by the way, as it can lead to an explosion
of services; a much better approach is to build one service that can handle
multiple workloads through configuration.)
Your Install() method should look somewhat like this:
public override void Install(IDictionary stateSaver) {
// There is only one service
ServiceInstaller serviceInstaller = new ServiceInstaller();
string serviceName = ConfigurationManager.AppSettings["ServiceName"];
if (string.IsNullOrEmpty(serviceName)) throw new
ConfigurationErrorsException("Missing 'ServiceName' setting in <appSettings>.");
serviceInstaller.ServiceName = serviceName;
string displayName = ConfigurationManager.AppSettings["DisplayName"];
if (displayName != null) serviceInstaller.DisplayName = displayName;
string description = ConfigurationManager.AppSettings["Description"];
if (description != null) serviceInstaller.Description = description;
Installers.Add(serviceInstaller);
base.Install(stateSaver);
}
ServiceInstaller does the heavy lifting for you. You can take the settings
from "stateSaver" instead of the configuration file. You can invent your own
keys for this -- I recommend using a prefix so you don't clash with the
settings the framework is going to be storing there.
Sample use of your project installer:
Hashtable savedState = new Hashtable();
projectInstaller.Context = new InstallContext(null, args);
projectInstaller.Context.Parameters["AssemblyPath"] =
Process.GetCurrentProcess().MainModule.FileName;
projectInstaller.Install(savedState);
The "AssemblyPath" parameter is used by ServiceInstaller to... well, you can
figure this one out, I'm sure.