8

I have added App.Config in my project. I have a installer class(ProjectInstaller.cs) which needs to read values from App.config. I am providing the keys . Below is the sample Code :

ConfigurationManager.AppSettings["CONFIG_FILE"]

I am getting null values as per above code ,when invoked in Installer class. But in App.Config file the value for the above key exists.

sachin kulkarni
  • 2,618
  • 7
  • 28
  • 33
  • See http://stackoverflow.com/questions/379276/windows-service-cant-access-app-config-from-within-my-installers-constructor for what's going on and the best fixes - you can't work with an app.config directly from an installer as your application isn't actually running yet. Rather, it's `installutil` that's running. Note, by the way, that as you're writing a question this site will suggest possible answers -- read them! – Jeremy McGee Oct 03 '11 at 10:45

4 Answers4

22

Try:

public string GetServiceNameAppConfig(string serviceName)
{
    var config = ConfigurationManager.OpenExeConfiguration(Assembly.GetAssembly(typeof(MyServiceInstaller)).Location);
    return config.AppSettings.Settings[serviceName].Value;
}
Marlon
  • 315
  • 2
  • 2
3

Google helps: http://social.msdn.microsoft.com/Forums/ar/winformssetup/thread/896e110e-692d-4934-b120-ecc99b01c562

the point is that your installer is NOT running as exe alone and an app.config called whatever you imagine will not be loaded by default as the exe running your installer is InstallUtil.exe and it would eventually search appSettings from the file InstallUtil.exe.config which is not yours and is not what you want, read the following and check the links...

If you invoke it through InstallUtil then the configuration file is defined as InstallUtil.exe.config which is not what you want. You could manually load the config file using Configuration but it will probably be a little messy

The trick is in the execution context of the installer classes. If you install your app using InstallUtil all code will be executed in the same process as InstallUtil.exe. If you need to pass some data to the Installer class during deployment you should use install parameters. They are passed to the installer during execution of Install, Commit, Rollback and Uninstall methods by the execution enviroment (installutil, windows instller...). You can access there parameters using InstallContex property ot the installer class.

There is a excellent artiicle on CodeProject regarding Setup projects and parameters: http://www.codeproject.com/dotnet/SetupAndDeployment.asp

Check out http://msdn2.microsoft.com/en-us/library/system.configuration.install.installcontext.aspx

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Thanks Davide, I have defined some important key values in my App.Config.I need to read them.Is there no way I could get it.Do I physically need to create a physical xml file. – sachin kulkarni Oct 03 '11 at 10:55
  • as I wrote in my answer, please read this one: http://www.codeproject.com/KB/install/SetupAndDeployment.aspx – Davide Piras Oct 03 '11 at 10:59
1

For me the easiest solution was to create InstallUtil.exe.config file, and fill it up with content from application config file. Service installer successfully read from this config file.

I created my service by following steps described in: Host a WCF Service in a Managed Windows Service

MBI
  • 583
  • 8
  • 22
1

Davide Piras explained very well, why you can't use your app.config and suggests to pass your values as parameters.

I found a nice and helpful article on how to pass parameters to the installutil.exe and use them in the serviceInstaller or projectInstaller:

Part 1: Using Parameters with InstallUtil

Part 2: Configuring Windows Services with Parameters from InstallUtil

It explains very shortly how to pass arguments and how to read them.

Jan
  • 2,747
  • 27
  • 35