Windows service installer not reading App.Config file

℡╲_俬逩灬. 提交于 2019-11-30 14:33:58

Try:

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!