Service 我在windows服务中遇到system.configuration.settingspropertynotfoundexception错误

Service 我在windows服务中遇到system.configuration.settingspropertynotfoundexception错误,service,windows-services,system.configuration,Service,Windows Services,System.configuration,我有一个windows应用程序,它使用SettingsProvider读取配置设置,并在文件不存在时设置默认值 工作正常,运转正常 我正在尝试编写启动此应用程序的windows服务。当它由服务运行时,我会在所有设置属性上获得System.Configuration.SettingsPropertyNotFoundException 当服务运行应用程序时,如何解决此异常?这意味着应用程序无法读取.Settings文件。我可以想到两个可能的原因: 该服务在没有访问.settings文件权限的帐户下运

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

工作正常,运转正常

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


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

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

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

  • 运行时找不到设置文件。它希望设置位于可执行文件的根启动路径中。检查以确保相关机器上存在该故障


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

    另一个可能的原因是您编写的自定义
    设置Provider
    初始化期间引发异常

    就我而言,我做到了:

    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);
        }