我在 Windows 服务中收到 system.configuration.settingspropertynotfoundexception 错误

发布于 2024-12-19 23:39:57 字数 240 浏览 8 评论 0 原文

我有一个 Windows 应用程序,它使用 SettingsProvider 读取配置设置并在文件不存在时设置默认值。

正常运行效果很好。

我正在尝试编写一个启动此应用程序的 Windows 服务。当它由服务运行时,我在所有设置属性上收到 System.Configuration.SettingsPropertyNotFoundException

当服务运行应用程序时,如何解决此异常?

I have a windows app that uses SettingsProvider to read configuration settings and sets default values if file does not exist.

It works fine running normally.

I am trying to write a windows service that starts this app. When it is run by the service, I get System.Configuration.SettingsPropertyNotFoundException on all the setting attributes.

How can I resolve this exception when the service is running the app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

家住魔仙堡 2024-12-26 23:39:57

这仅意味着应用程序无法读取 .Settings 文件。我可以想到两个可能的原因:

  1. 该服务在无权访问 .settings 文件的帐户下运行。 (或 .config 文件,具体取决于)这不太可能,因为服务可以启动应用程序,并且它拥有应用程序的权限而不是设置文件的权限是没有意义的。

  2. 运行时无法找到设置文件。它期望设置位于可执行文件的根启动路径中。检查以确保它存在于相关计算机上。

然而,谷歌的结果出现了一个我没有想到的明显的可能原因。 .settings 是在上次编译后添加的吗?在 Visual Studio 中编译应用程序,然后重试...

This simply means that the app can't read the .Settings file. I can think of two possible causes:

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

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

〃安静 2024-12-26 23:39:57

另一个可能的原因是,如果您编写了一个在初始化期间抛出异常的自定义SettingsProvider

就我而言,我这样做了:

public class CustomSettingsProvider : SettingsProvider
{
    public override void Initialize(string name, NameValueCollection config)
    {
        base.Initialize(name, config);
    }
}

由于 name 始终作为 null 传递,因此 base.Initialize 抛出 ArgumentNullException代码>.我通过传递一个非空名称来修复它,如下所示:

    public override void Initialize(string name, NameValueCollection config)
    {
        base.Initialize(name ?? GetType().Name, config);
    }

Another possible cause is if you write a custom SettingsProvider that is throwing and exception during Initialize.

In my case, I had done this:

public class CustomSettingsProvider : SettingsProvider
{
    public override void Initialize(string name, NameValueCollection config)
    {
        base.Initialize(name, config);
    }
}

Since name is always passed as null, base.Initialize was throwing an ArgumentNullException. I fixed it by passing a non-null name like this:

    public override void Initialize(string name, NameValueCollection config)
    {
        base.Initialize(name ?? GetType().Name, config);
    }
淡笑忘祈一世凡恋 2024-12-26 23:39:57

就我而言,配置文件 代码在 Web.config 文件中被注释掉。

取消注释上面的行对我有用。

In my case, the profile <configSource="Profile.config" /> code was commented out in the Web.config file.

Uncommenting the above line worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文