Windows 服务安装程序不读取 App.Config 文件
我已经在我的项目中添加了 App.Config 。 我有一个安装程序类(ProjectInstaller.cs),它需要从 App.config 读取值。 我正在提供钥匙。 下面是示例代码:
ConfigurationManager.AppSettings["CONFIG_FILE"]
当在 Installer 类中调用时,我按照上面的代码获取空值。 但在 App.Config 文件中存在上述键的值。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
Try:
谷歌帮助:http://social.msdn.microsoft.com/Forums/ar/winformssetup/thread/896e110e-692d-4934-b120-ecc99b01c562
要点是您的安装程序不是 单独作为 exe 运行,并且默认情况下不会加载名为您想象的任何名称的 app.config,因为运行安装程序的 exe 是InstallUtil.exe,它最终会从文件 InstallUtil.exe.config 中搜索 appSettings,该文件不是您的,也不是您想要的,请阅读以下内容并检查链接。 ..
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...
Davide Piras 解释得很好,为什么你不能使用你的 app.config 并建议将你的值作为参数传递。
我发现了一篇不错且有用的文章,介绍了如何将参数传递给
installutil.exe
并在serviceInstaller
或projectInstaller
中使用它们:第 1 部分: 使用参数InstallUtil
第 2 部分:使用 InstallUtil 中的参数配置 Windows 服务
它非常简短地解释了如何传递参数以及如何读取它们。
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 theserviceInstaller
orprojectInstaller
: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.
对我来说,最简单的解决方案是创建 InstallUtil.exe.config 文件,并用应用程序配置文件中的内容填充它。服务安装程序已成功从此配置文件读取。
我按照以下描述的步骤创建了服务: 在托管 Windows 服务中托管 WCF 服务
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