5

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!!!

1
  • When installUtil is running, your app.config isn't used by the configuratio manager - it used installUtil's app.config (under c:\windows\Microsoft.NET\Framework(fw_number)\installUtil.exe.config Commented May 27, 2013 at 8:16

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou very much for your answer. In my case I prefer the solution posted by C4stor 'cause it looks more reliable to me. But I want to let you know that your approach sure is a valid one to me.
2

Faced with this problem, I first used the approach here :

But I then switched back to an architecture where I have a single service launching a variable amount of processes (in my case, I use zookeeper to set the number of processes to run and their command line arguments, but if it's just a matter of a number of instances, app.config is good to go).

The main advantage I found in that way is that my launching service can monitor the running sub processes and, for example, relaunch one if it has crashed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.