【问题标题】:App.config - custom section not workingApp.config - 自定义部分不起作用
【发布时间】:2010-09-23 07:45:25
【问题描述】:

我最近开始构建 Web 应用程序的控制台版本。我从我的 web.config 复制了我的自定义部分。到我的 app.config。当我去获取配置信息时,我得到了这个错误:

为 x/y 创建配置节处理程序时出错:无法从程序集“System.Configuration”加载类型“x”

它不喜欢的行是:

return ConfigurationManager.GetSection("X/Y") as Z;

有人遇到过这种情况吗?

我可以添加

<add key="IsReadable" value="0"/> 

在 appSettings 中并阅读它。

加法:

我确实有关于自定义部分的定义:

  <configSections>
    <sectionGroup name="x">
      <section name="y" type="zzzzz"/>
    </sectionGroup>

  </configSections>

【问题讨论】:

    标签: c#


    【解决方案1】:

    听起来你的配置节处理程序没有定义

    <configSection>
        <section
                name="YOUR_CLASS_NAME_HERE"
                type="YOUR.NAMESPACE.CLASSNAME, YOUR.NAMESPACE, Version=1.1.0.0, Culture=neutral, PublicKeyToken=PUBLIC_TOKEN_ID_FROM_ASSEMBLY"
                allowLocation="true"
                allowDefinition="Everywhere"
              />
    </configSection>
    

    【讨论】:

    • 该类型部分需要多少?
    • 您不需要 Culture 和 PublicKey 来解决您的问题。在下面查看我的简短版本。
    • @Daok:我只是听从命令! ;-)
    【解决方案2】:

    如果您想要自定义配置处理程序,您必须定义类并引用它,如 Steven Lowe 所示。您可以从预定义的处理程序继承,或者您可以只使用 appSetting 部分中提供的值/键对,如您所述。

    【讨论】:

      【解决方案3】:

      在文件的顶部,您需要在部分内包含 configSection 标记。

      您也可以拥有 sectionGroup。示例:

      <configuration>
         <configSections>
           <sectionGroup name="x">
             <section name="y" type="a, b"/>
           </sectionGroup>
          <configSections>
      </configuration>
      

      【讨论】:

        【解决方案4】:

        这个类可以作为任何类型的通用自定义配置节处理程序......

        public class XmlConfigurator : IConfigurationSectionHandler
            {
                public object Create(object parent, object configContext, XmlNode section)
                {
                    if (section == null) return null;
                    Type sectionType = Type.GetType((string)(section.CreateNavigator()).Evaluate("string(@configType)"));
                    XmlSerializer xs = new XmlSerializer(sectionType);
                    return xs.Deserialize(new XmlNodeReader(section));
                }
            }
        

        在您的 app.config 中,添加

          <section name="NameofConfigSection"  type="NameSpace.XmlConfigurator, NameSpace.Assembly"/>
        

        并且在配置节元素中,添加一个属性来指定你希望根元素反序列化成的类型..

        <?xml version="1.0" encoding="utf-8" ?>
        
        <NameofConfigSection configType="NameSpace.NameofTypeToDeserializeInto, Namespace.Assembly" >
        
         ... 
        
        </NameofConfigSection>
        

        【讨论】:

        • IConfigurationSectionHandler 在 .NET 2.0 及更高版本中已弃用。而是从 ConfigurationSection 继承。
        • 但是,据我所知,您无法使用更新的方法...使用 ConfigurationSection,您必须为要反序列化的每个新配置部分编写一个新的和不同的派生类...
        【解决方案5】:

        我最近遇到了同样的问题。我为 Web 应用程序创建了一个自定义部分组(运行得很好),但是当我将此层移植到控制台应用程序时,部分组失败了。

        您关于部分定义中需要多少“类型”的问题是正确的。我用下面的例子修改了你的配置部分:

        <configSection>
            <section
                name="yourClassName"
                type="your.namespace.className, your.assembly"
                allowLocation="true"
                allowDefinition="Everywhere" />
        </configSection>
        

        您会注意到,该类型现在具有类名,后跟程序集名。这是在网络环境之外的交互所必需的。

        注意:程序集名称不一定等于您的命名空间(对于给定的部分)。

        【讨论】:

          猜你喜欢
          • 2012-10-28
          • 1970-01-01
          • 2011-01-30
          • 2011-10-02
          • 2020-06-29
          • 2015-07-05
          • 2011-05-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多