Csharp/C#教程:无法为web.config创建自定义部分分享


无法为web.config创建自定义部分

我在web.config文件中创建了一个自定义部分,但它无法加载我要管理该部分的自定义类型。

以下是定义:

 
namespace MyApp.BusinessObjects { public class MembershipProviderFactory { internal virtual IMembershipProvider Create() { } public class MembershipProvidersSection : ConfigurationSection { public class AddElement: ConfigurationElement { [ConfigurationProperty("name", IsKey = true, IsRequired = true)] public string Name { get { return this["name"].ToString(); } set { this["name"] = value; } } [ConfigurationProperty("type", IsRequired = true)] public string FullyQualifiedTypeName { get { return this["type"].ToString(); } set { this["type"] = value; } } } public class AddElementCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new AddElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((AddElement)element).Name; } } [ConfigurationProperty("currentProvider", IsRequired = false)] public string CurrentProvider { get { return this["currentProvider"].ToString(); } set { this["currentProvider"] = value; } } [ConfigurationProperty("add", IsRequired = true, IsDefaultCollection = true)] public AddElementCollection Instances { get { return (AddElementCollection)this["add"]; } set { this["add"] = value; } } } } }

我得到一个运行时exception,说:

为MembershipProviders创建配置节处理程序时发生错误:无法加载类型“MyApp.BusinessObjects.MembershipProviderFactory.MembershipProvidersSection”。

更新

我还在配置文件中包含了实际部分,如下所示:

  

我仍然得到同样的例外。

您需要将程序集名称指定为type属性的一部分:

 

编辑
我没注意到MembershipProvidersSection类是一个嵌套类。
类型名称应为:

 MyApp.BusinessObjects.MembershipProviderFactory+MembershipProvidersSection 

您在声明类型时缺少程序集名称:

 MyApp.BusinessObjects.MembershipProviderFactory.MembershipProvidersSection,? 

看一下我关于自定义配置的post: C#WCF System.Configuration.ConfigurationErrorsException:无法识别的元素’ManagedService’

上述就是C#学习教程:无法为web.config创建自定义部分分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/959349.html

(0)
上一篇 2021年11月23日
下一篇 2021年11月23日

精彩推荐