我在 Windows 服务中收到 system.configuration.settingspropertynotfoundexception 错误
我有一个 Windows 应用程序,它使用 SettingsProvider 读取配置设置并在文件不存在时设置默认值。
正常运行效果很好。
我正在尝试编写一个启动此应用程序的 Windows 服务。当它由服务运行时,我在所有设置属性上收到 System.Configuration.SettingsPropertyNotFoundException
。
当服务运行应用程序时,如何解决此异常?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这仅意味着应用程序无法读取 .Settings 文件。我可以想到两个可能的原因:
该服务在无权访问 .settings 文件的帐户下运行。 (或 .config 文件,具体取决于)这不太可能,因为服务可以启动应用程序,并且它拥有应用程序的权限而不是设置文件的权限是没有意义的。
运行时无法找到设置文件。它期望设置位于可执行文件的根启动路径中。检查以确保它存在于相关计算机上。
然而,谷歌的结果出现了一个我没有想到的明显的可能原因。 .settings 是在上次编译后添加的吗?在 Visual Studio 中编译应用程序,然后重试...
This simply means that the app can't read the .Settings file. I can think of two possible causes:
The service runs under an account that doesn't have access to the .settings file. (or .config file, depending) This is unlikely because the service can start the app, and it wouldn't make sense for it to have permissions to the app and not the settings file.
The runtime can't find the settings file. It expects the settings to be in the root startup path of the executable. Check to ensure that it exists on the machine in question.
However, a google result turned up an obvious possible cause I haven't thought of. Were the .settings added after the last compile? Compile the app in Visual Studio, and try again...
另一个可能的原因是,如果您编写了一个在
初始化
期间抛出异常的自定义SettingsProvider
。就我而言,我这样做了:
由于
name
始终作为null
传递,因此base.Initialize
抛出ArgumentNullException
代码>.我通过传递一个非空名称来修复它,如下所示:Another possible cause is if you write a custom
SettingsProvider
that is throwing and exception duringInitialize
.In my case, I had done this:
Since
name
is always passed asnull
,base.Initialize
was throwing anArgumentNullException
. I fixed it by passing a non-null name like this:就我而言,配置文件
代码在Web.config
文件中被注释掉。取消注释上面的行对我有用。
In my case, the profile
<configSource="Profile.config" />
code was commented out in theWeb.config
file.Uncommenting the above line worked for me.