I get a system.configuration.settingspropertynotfoundexception error in windows service

一曲冷凌霜 提交于 2019-12-12 17:26:01

问题


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?


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/8404832/i-get-a-system-configuration-settingspropertynotfoundexception-error-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!