Windows Service Installer Does Not Read App.Config File

I added App.Config to my project. I have an installer class (ProjectInstaller.cs) that needs to read values โ€‹โ€‹from App.config. I provide the keys. The following is sample code:

ConfigurationManager.AppSettings["CONFIG_FILE"] 

I get null values โ€‹โ€‹according to the code above when called in the Installer class. But in the App.Config file there is a value for the specified key.

+6
source share
4 answers

Try:

 public string GetServiceNameAppConfig(string serviceName) { var config = ConfigurationManager.OpenExeConfiguration(Assembly.GetAssembly(typeof(MyServiceInstaller)).Location); return config.AppSettings.Settings[serviceName].Value; } 
+14
source

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

The fact is that your installer does NOT work as exe separately, and app.config called what you imagine will not load by default, because exe with your installer InstallUtil.exe , and ultimately it will look for appSettings from the InstallUtil.exe.config file, which is not yours and not what you want, read the following and check the links ...

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

The trick is in the context of executing installer classes. if you install the application using InstallUtil, all the code will be executed in the same process as InstallUtil.exe. If you need to transfer some data to the Installer Class during deployment, you must use the installation options. They are passed to the installer at run time Install, Commit, Chip and delete methods using the runtime (installutil, windows instller ...). You can access these parameters using the InstallContex property of the installer class.

CodeProject has an excellent article on installation projects and options: http://www.codeproject.com/dotnet/SetupAndDeployment.asp

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

+3
source

David Piras explained very well why you cannot use your app.config and suggests passing your values โ€‹โ€‹as parameters.

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

Part 1: Using Options with InstallUtil

Part 2: Configuring Windows Services with Settings from InstallUtil

It briefly explains how to pass arguments and how to read them.

+1
source

For me, the easiest solution was to create the InstallUtil.exe.config file and fill it with the contents from the application configuration file. The service installer has successfully read from this configuration file.

I created my service by following these steps: WCF service host in a managed Windows service

0
source

Source: https://habr.com/ru/post/898507/


All Articles