开发者

Windows service installer not reading App.Config file

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 i开发者_Python百科n App.Config file the value for the above key exists.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜