custom section in app.config throws error in c#

2019-08-10 19:33发布

问题:

// WinConfiguration.cs

using System;
using System.Configuration;

namespace BANANA.Common
{
    public class WinConfiguration : ConfigurationSection
    {
        public readonly static BANANA.Common.WinConfiguration Settings
            = (BANANA.Common.WinConfiguration)System.Configuration.ConfigurationManager.GetSection("BANANA");

        [ConfigurationProperty("connections")]
        public ConnectionsCollection Connections
        {
            get { return (ConnectionsCollection)this["connections"]; }
        }

        [ConfigurationProperty("cryptography")]
        public CryptographyCollection Cryptography
        {
            get { return (CryptographyCollection)this["cryptography"]; }
        }
    }
}

// app.config

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="BANANA" type="BANANA.Common.WinConfiguration"/>
    </configSections>
    <BANANA>
        <connections>
            <add name="SqlServer2008" connectionString="" providerName="System.Data.SqlClient" priority="100" />
        </connections>
        <cryptography>
            <add type="DES" value="12345" />
            <add type="TripleDES" value="12345" />
        </cryptography>
    </BANANA>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>

It used to work in web.config perfectly. I just changed the class name. Here is my web.config and custom configuration class.

// WebConfiguration.cs

using System;
using System.Configuration;

namespace BANANA.Common
{
    public class WebConfiguration : ConfigurationSection
    {
        public readonly static BANANA.Common.WebConfiguration Settings = (BANANA.Common.WebConfiguration)System.Web.Configuration.WebConfigurationManager.GetSection("BANANA");

        [ConfigurationProperty("connections")]
        public ConnectionsCollection Connections
        {
            get { return (ConnectionsCollection)this["connections"]; }
        }

        [ConfigurationProperty("cryptography")]
        public CryptographyCollection Cryptography
        {
            get { return (CryptographyCollection)this["cryptography"]; }
        }
    }
}

// web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="BANANA" type="BANANA.Common.WebConfiguration"/>
    </configSections>
    <BANANA>
        <connections>
            <add name="SqlServer2008" connectionString="" providerName="System.Data.SqlClient" priority="100" />
        </connections>
        <cryptography>
            <add type="DES" value="12345" />
            <add type="TripleDES" value="12345" />
        </cryptography>
    </BANANA>
</configuration>

It just works fine with WebConfiguration.cs and web.config. But with WinConfiguration.cs and app.config, it gives me some error like "The type initializer for 'BANANA.Common.WinConfiguration' threw an exception.". Can anyone fix this error for me?

回答1:

I think that you have to specify the assembly name (the name of the EXE or DLL) where the configuration class resides:

<section name="BANANA" type="BANANA.Common.WinConfiguration, YOUR_ASSEMBLY_NAME"/>