Configuration 读取自定义配置节时出错:未为此对象定义无参数构造函数
从web.config读取自定义配置节时遇到了麻烦: 我正在使用配置节设计器() 更新: 以下是我得到的错误: System.Configuration.ConfigurationErrorsException:创建SendToTestConfig/sendToTestIndexConfig的配置节处理程序时出错:未为此对象定义无参数构造函数。(C:\TFS\Mainline\Business.Utility.SendToTest\Business.Utility.SendToTest\web.config第20行)--->System.MissingMethodException:没有为此对象定义无参数构造函数。 在System.RuntimeTypeHandle.CreateInstance(RuntimeType类型、Boolean publicOnly、Boolean noCheck、Boolean&canBeCached、RuntimeMethodHandle&ctor、Boolean&bNeedSecurityCheck) 位于System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔fillCache) 位于System.RuntimeType.CreateInstanceImpl(布尔publicOnly、布尔skipVisibilityChecks、布尔fillCache) 以下是我的自动生成配置部分:Configuration 读取自定义配置节时出错:未为此对象定义无参数构造函数,configuration,web-config,configurationsection,custom-configuration,Configuration,Web Config,Configurationsection,Custom Configuration,从web.config读取自定义配置节时遇到了麻烦: 我正在使用配置节设计器() 更新: 以下是我得到的错误: System.Configuration.ConfigurationErrorsException:创建SendToTestConfig/sendToTestIndexConfig的配置节处理程序时出错:未为此对象定义无参数构造函数。(C:\TFS\Mainline\Business.Utility.SendToTest\Business.Utility.SendToTest\web.
我猜这和我的名字有关。ConfigurationManager.AppSettings工作正常,因此我知道我有正确的web.config。我不确定这是否是最佳解决方案,但我能够解决这个问题。错误是因为我使用
GenericEnumTypeConverter
类将配置字符串转换为AppGroup
和子字段类型
枚举。我创建了自己的自定义类型转换器,它解决了这个问题。显然,GenericEnumTypeConverter
没有无参数构造函数,构造函数需要枚举类型。我很想知道是否有一种方法可以使用GenericEnumTypeConverter
,但这对我很有用
这让我得出了答案:事实上,问题的解决方案已在上的
GenericEnumConverter
示例中简要说明。我做了与您可能做的相同的事情,在我的一个配置节属性上显式设置了[TypeConverter(typeof(GenericEnumConverter))]
attrbibute,并得到了相同的错误。根据上面链接的GenericEnumConverter
文档,使用GenericEnumConverter
类型转换器不需要设置此属性——它由框架隐式调用。从您的配置属性规范中删除该属性,这个错误就会消失,一切正常
下面是使用枚举的配置节属性的示例:
public enum UsernameFormat
{
DownLevelDomainName,
UsernameOnly,
UserPrincipalName
}
public class WindowsADElement : ConfigurationElement
{
// This property will implicitly use the GenericEnumConverter type converter.
[ConfigurationProperty("usernameFormat", IsRequired=true, DefaultValue=UsernameFormat.UserPrincipalName)]
public UsernameFormat UsernameFormat
{
get { return (UsernameFormat)this["usernameFormat"]; }
set { this["usernameFormat"] = value; }
}
然后,要在代码中使用它:
MyConfigurationSection config = ConfigurationManager.GetSection("myConfigurationSection") as MyConfigurationSection;
UsernameFormat format = config.UsernameLookup.WindowsAD.UsernameFormat;
希望这会有帮助。确实有帮助。一旦我删除了GenericEnumConverter,它就开始工作了。
//These return null:
SendToTestIndexConfig config = SendToTestIndexConfig.Instance;
//SendToTestIndexConfig config = (SendToTestIndexConfig) ConfigurationManager.GetSection("sendToTestIndexConfig");
//SendToTestIndexConfig configb = (SendToTestIndexConfig)WebConfigurationManager.GetSection("sendToTestIndexConfig");
//SendToTestIndexConfig configc = (SendToTestIndexConfig)WebConfigurationManager.OpenWebConfiguration(null).GetSection("sendToTestIndexConfig");
//SendToTestIndexConfig configd = (SendToTestIndexConfig)WebConfigurationManager.GetWebApplicationSection("sendToTestIndexConfig");
//SendToTestIndexConfig configf = (SendToTestIndexConfig)WebConfigurationManager.GetSection("sendToTestIndexConfig");
////These throw a "parameterless constructor error" on object "SendToTestConfig/sendToTestIndexConfig"
//SendToTestIndexConfig configg = (SendToTestIndexConfig)WebConfigurationManager.GetSection("SendToTestConfig/sendToTestIndexConfig");
//SendToTestIndexConfig configh = (SendToTestIndexConfig)WebConfigurationManager.OpenWebConfiguration(null).GetSection("SendToTestConfig/sendToTestIndexConfig");
//SendToTestIndexConfig configi = (SendToTestIndexConfig)WebConfigurationManager.GetWebApplicationSection("SendToTestConfig/sendToTestIndexConfig");
//SendToTestIndexConfig configj = (SendToTestIndexConfig)WebConfigurationManager.GetSection("SendToTestConfig/sendToTestIndexConfig");
public enum UsernameFormat
{
DownLevelDomainName,
UsernameOnly,
UserPrincipalName
}
public class WindowsADElement : ConfigurationElement
{
// This property will implicitly use the GenericEnumConverter type converter.
[ConfigurationProperty("usernameFormat", IsRequired=true, DefaultValue=UsernameFormat.UserPrincipalName)]
public UsernameFormat UsernameFormat
{
get { return (UsernameFormat)this["usernameFormat"]; }
set { this["usernameFormat"] = value; }
}
MyConfigurationSection config = ConfigurationManager.GetSection("myConfigurationSection") as MyConfigurationSection;
UsernameFormat format = config.UsernameLookup.WindowsAD.UsernameFormat;