Re: How to install a windows service programmatically



"Jeroen Mostert" wrote:

Bodo wrote:
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. :-)

Hi Jeroen,
thanks for pointing me to the right direction.
Withthe use of AssemblyInstaller class I'm now able to install my assemby.
However I'm missing one point, thats the Parameters required by the assembly
Windows registry entry for that service has a "Parameters" Key.
The assembly requires some string values that are stored in the Parameters
Key.

Are there any framework methods that allows me to add the values to the
Parameters Key on installation process other than adding them by
Microsoft.Win32.RegistryKey ?

Again many thanks,
Bodo



.



Relevant Pages

  • Re: How to install a windows service programmatically
    ... 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 Installmethod. ...
    (microsoft.public.dotnet.languages.csharp)
  • My installed Windows Service does not show in the list of installed services !
    ... windows service in the dotnet world (I have done some with the old VB6 ... Beginning the Install phase of the installation. ... The Commit phase completed successfully. ...
    (microsoft.public.dotnet.general)
  • Windows Service
    ... i tried to create a very simple Windows Service using C#, after i install the exe as Windows Service by using installutil ... ... Beginning the Install phase of the installation. ... and the Commit phase is beginning. ...
    (microsoft.public.dotnet.languages.csharp)
  • Install Windows Service with Custom User Credentials
    ... I want to achieve is Install a Service with Customised parameters (using ... at System.ServiceProcess.ServiceInstaller.Install(IDictionary stateSaver) ... The Rollback phase of the installation is beginning. ... but using passed custom values to the Install Utility ...
    (microsoft.public.dotnet.general)
  • Re: MSI "Repair" destroys installstate of custom installer
    ... the debugger shows that the stateSaver collection is empty (or at least ... MyInstaller.Installwhen doing a repair. ... way to exit Install() immediately when a repair install is triggered. ... | on your custom action should help (Installed is case sensitive - it's ...
    (microsoft.public.vsnet.setup)