vb.net Windows 服务无法从 App.config 读取参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19119041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows service cannot read parameters from the App.config
提问by Benjamin Beaulieu
I created a Windows service used to execute automated procedures (.Net code) for multiple periodic operations, like backups, sanity checks, reports generation, etc. After building the project, I installed the service with installutil. Everything worked great.
我创建了一个 Windows 服务,用于执行多个定期操作的自动化程序(.Net 代码),例如备份、健全性检查、报告生成等。构建项目后,我使用 .Net 安装了该服务installutil。一切都很好。
I decided to move various "static" parameters for these automated procedures in the App.configfile. I uninstalled the previous version of the service with installutil /uand built the new version of the project. In my build output folder, there's a AppName.exefile and a AppName.exe.configfile, as I would expect. I installed the new version of the service, again with installutilfrom the VS 2012 Developer Command Prompt as an administrator.
我决定在App.config文件中为这些自动化程序移动各种“静态”参数。我卸载了该服务的先前版本installutil /u并构建了该项目的新版本。正如我所料,在我的构建输出文件夹中,有一个AppName.exe文件和一个AppName.exe.config文件。我安装了新版本的服务,再次以installutil管理员身份从 VS 2012 开发人员命令提示符安装。
The problem is that the service doesn't seem to be able to read the configuration file from the ConfigurationManager. The call for ConfigurationManager.AppSettings("paramname")doesn't fail, but the resulting parameter value is an empty string. As far as I know, the problem occurs for all parameters and not only for specific ones. The parameters are located in the <appSettings>section, under <configuration>, like I've done multiple times before in various projects.
问题是该服务似乎无法从ConfigurationManager. 调用ConfigurationManager.AppSettings("paramname")不会失败,但结果参数值是一个空字符串。据我所知,所有参数都会出现问题,而不仅仅是特定参数。参数位于<appSettings>下的部分,<configuration>就像我之前在各种项目中做过多次一样。
I don't know if it can help, but my service runs on the LocalSystemaccount and starts automatically after installation and with Windows.
我不知道它是否有帮助,但我的服务在该LocalSystem帐户上运行,并在安装和 Windows 后自动启动。
What did I do wrong?
我做错了什么?
Edit: I already tried uninstalling/re-installing the service (multiple times), like some stackoverflow answers suggests. Also, I'm not looking to update/refresh the file at runtime.
编辑:我已经尝试卸载/重新安装服务(多次),就像一些 stackoverflow 的答案所暗示的那样。另外,我不希望在运行时更新/刷新文件。
采纳答案by Benjamin Beaulieu
Turns out I was getting the configuration value in a variable declared with the same name as the property I was using to store the value. For an unknown reason, I had no warning from the compiler. Since a Windows service cannot be debugged, the solution was to review the code/rewrite this part from scratch, and that's when I saw the error.
结果我在一个变量中获取配置值,该变量声明的名称与我用来存储值的属性同名。由于未知原因,我没有收到编译器的警告。由于无法调试Windows服务,因此解决方案是从头开始查看代码/重写这部分,这就是我看到错误的时候。
回答by FredrikR
I solved this by setting the location of the config file in runtime. This enables me to place and name the config file where ever I want. I fetch the executing assembly path and then checks if the config file is there. Check the answer on this thread how to dynamically set the config file
我通过在运行时设置配置文件的位置解决了这个问题。这使我能够在任何我想要的地方放置和命名配置文件。我获取正在执行的程序集路径,然后检查配置文件是否在那里。检查此线程上的答案如何动态设置配置文件
This is how I fetch the executing assembly:
这是我获取执行程序集的方式:
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var configFilePath = Path.Combine(path, "service.config");
Hope this helps!
希望这可以帮助!

