user4266661
user4266661

Reputation:

ConfigurationErrorException on null DateTime

I'm suddenly running into a ConfigurationErrorException for a WPF app which was running fine for a number of months (it's a task tray application).

The exception is being thrown by this auto-generated code in Settings.Designer.cs:

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public global::System.DateTime LastBackup {
        get {
            return ((global::System.DateTime)(this["LastBackup"]));
        }
        set {
            this["LastBackup"] = value;
        }
    }

When the app first runs after being installed LastBackup is undefined/null/empty, which is what's causing the exception.

Interestingly, the auto-generated code lacks a [global::System.Configuration.DefaultSettingValueAttribute("")] attribute, which all the other auto-generated properties have.

If this was my own code it'd be easy enough to fix. But since it's generated by the Settings subsystem, any change I make would be overwritten.

There are a number of ways to work around this problem, including abandoning the built-in Settings subsystem and rolling my own configuration system. But I'm curious as to other approaches used to deal with undefined or null settings.

Upvotes: 0

Views: 56

Answers (1)

I was able to reproduce this error by altering App.config.

This is the default userSettings section I get when I create setting (named "Setting", sorry for the lack of imagination) in the VS Settings editor with an empty "Value" cell.

<userSettings>
    <WPFTreeViewItemWrap.Properties.Settings>
        <setting name="Setting" serializeAs="String">
            <value />
        </setting>
    </WPFTreeViewItemWrap.Properties.Settings>
</userSettings>

This returns a non-null DateTime equal to {1/1/0001 12:00:00 AM}:

var x = Properties.Settings.Default.Setting;

But I get the same exception as you if I remove the empty <value /> element, like so:

<userSettings>
    <WPFTreeViewItemWrap.Properties.Settings>
        <setting name="Setting" serializeAs="String"></setting>
    </WPFTreeViewItemWrap.Properties.Settings>
</userSettings>

System.Configuration.ConfigurationErrorsException: 'Required attribute 'value' not found.'

I would look at App.config. It may have been changed.

Upvotes: 0

Related Questions