Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass arguments to `Configuration.Install.Installer` when installing a long-running service through `installutil`

I have an application that I want to run as a Windows Service. Following instructions provided in this MSDN doc page (I need to host a WCF service, so the procedure also details this part), I can do that, and if I implement the example service it is all right. I use installutil.exe utility and can install and uninstall my application as a Windows Service.

My problem

However I need to install more service of the same application on my local machine. So I need to give them different System.ServiceProcess.ServiceBase.ServiceNames! So consider again the installation code:

[RunInstaller(true)]
public class ProjectInstaller : Installer {
    private ServiceProcessInstaller process;
    private ServiceInstaller service;

    public ProjectInstaller() {
        process = new ServiceProcessInstaller();
        process.Account = ServiceAccount.LocalSystem;
        service = new ServiceInstaller();
        service.ServiceName = /* NEED TO PUT HERE THE NAME!!! */;
        Installers.Add(process);
        Installers.Add(service);
    }
}

Is there a way for me to pass the name of the service when calling installutil.exe? How to approach to this problem? I also tried using the App.Config file and doing something like this:

public ProjectInstaller() {
    process = new ServiceProcessInstaller();
    process.Account = ServiceAccount.LocalSystem;
    service = new ServiceInstaller();
    service.ServiceName = System.Configuration.ConfigurationManager.
        AppSettings["SrvName"];
    Installers.Add(process);
    Installers.Add(service);
}

But of course it did not work, this file is called when the application runs!!!

like image 895
Andry Avatar asked May 27 '13 08:05

Andry


1 Answers

You can open config file for executing assembly. If your installer code is placed into main service exe file - it will be your app.config. Otherwise, config file need to be named [assemblyname].dll.config.

process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();

var path = Assembly.GetExecutingAssembly().Location;
var config = ConfigurationManager.OpenExeConfiguration(path);
service.ServiceName = config.AppSettings.Settings["ServiceName"];
Installers.Add(process);
Installers.Add(service);

Also, this article explains how to pass installutil parameters explicitly through command line.

like image 94
Alexander Avatar answered Sep 29 '22 05:09

Alexander