【问题标题】:Error reading Custom Configuration Section: No parameterless constructor defined for this object读取自定义配置部分时出错:没有为此对象定义无参数构造函数
【发布时间】:2012-09-24 21:06:59
【问题描述】:

从 web.config 中读取自定义配置部分非常痛苦: 我正在使用配置部分设计器 (http://csd.codeplex.com/)。

更新:

这是我得到的错误:

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,布尔填充缓存) 在 System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)

这是我自动生成的配置部分:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.225
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Business.Utility.SendToTest.Configuration
{


    /// <summary>
    /// The SendToTestIndexConfig Configuration Section.
    /// </summary>
    public partial class SendToTestIndexConfig : global::System.Configuration.ConfigurationSection
    {


        #region Singleton Instance
        /// <summary>
        /// The XML name of the SendToTestIndexConfig Configuration Section.
        /// </summary>
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("ConfigurationSectionDesigner.CsdFileGenerator", "2.0.0.0")]
        internal const string SendToTestIndexConfigSectionName = "sendToTestIndexConfig";

        /// <summary>
        /// Gets the SendToTestIndexConfig instance.
        /// </summary>
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("ConfigurationSectionDesigner.CsdFileGenerator", "2.0.0.0")]
        public static global::Business.Utility.SendToTest.Configuration.SendToTestIndexConfig Instance
        {
            get
            {
                return ((global::Business.Utility.SendToTest.Configuration.SendToTestIndexConfig)(global::System.Configuration.ConfigurationManager.GetSection(global::Business.Utility.SendToTest.Configuration.SendToTestIndexConfig.SendToTestIndexConfigSectionName)));
            }
        }
        #endregion

        #region Xmlns Property
        /// <summary>
        /// The XML name of the <see cref="Xmlns"/> property.
        /// </summary>
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("ConfigurationSectionDesigner.CsdFileGenerator", "2.0.0.0")]
        internal const string XmlnsPropertyName = "xmlns";

        /// <summary>
        /// Gets the XML namespace of this Configuration Section.
        /// </summary>
        /// <remarks>
        /// This property makes sure that if the configuration file contains the XML namespace,
        /// the parser doesn't throw an exception because it encounters the unknown "xmlns" attribute.
        /// </remarks>
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("ConfigurationSectionDesigner.CsdFileGenerator", "2.0.0.0")]
        [global::System.Configuration.ConfigurationPropertyAttribute(global::Business.Utility.SendToTest.Configuration.SendToTestIndexConfig.XmlnsPropertyName, IsRequired=false, IsKey=false, IsDefaultCollection=false)]
        public string Xmlns
        {
            get
            {
                return ((string)(base[global::Business.Utility.SendToTest.Configuration.SendToTestIndexConfig.XmlnsPropertyName]));
            }
        }
        #endregion

        #region IsReadOnly override
        /// <summary>
        /// Gets a value indicating whether the element is read-only.
        /// </summary>
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("ConfigurationSectionDesigner.CsdFileGenerator", "2.0.0.0")]
        public override bool IsReadOnly()
        {
            return false;
        }
        #endregion

        #region appGroups Property
        /// <summary>
        /// The XML name of the <see cref="appGroups"/> property.
        /// </summary>
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("ConfigurationSectionDesigner.CsdFileGenerator", "2.0.0.0")]
        internal const string appGroupsPropertyName = "appGroups";

        /// <summary>
        /// Gets or sets the appGroups.
        /// </summary>
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("ConfigurationSectionDesigner.CsdFileGenerator", "2.0.0.0")]
        [global::System.ComponentModel.DescriptionAttribute("The appGroups.")]
        [global::System.Configuration.ConfigurationPropertyAttribute(global::Business.Utility.SendToTest.Configuration.SendToTestIndexConfig.appGroupsPropertyName, IsRequired=true, IsKey=false, IsDefaultCollection=false)]
        public global::Business.Utility.SendToTest.Configuration.AppGroupSettingsCollection appGroups
        {
            get
            {
                return ((global::Business.Utility.SendToTest.Configuration.AppGroupSettingsCollection)(base[global::Business.Utility.SendToTest.Configuration.SendToTestIndexConfig.appGroupsPropertyName]));
            }
            set
            {
                base[global::Business.Utility.SendToTest.Configuration.SendToTestIndexConfig.appGroupsPropertyName] = value;
            }
        }
        #endregion
    }
}
namespace Business.Utility.SendToTest.Configuration
{
   ... 

这是我的配置文件:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="SendToTestConfig" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
      <section name="sendToTestIndexConfig" type="Business.Utility.SendToTest.Configuration.SendToTestIndexConfig, Business.Utility.SendToTest.Configuration"/>
    </sectionGroup>
etc.

  </configSections>
    <SendToTestConfig xmlns="Business.Utility.SendToTest.Configuration">
      <sendToTestIndexConfig>
        <appGroups>
          <!-- TODO Change indexFileNamePrefix to desired value-->
          <appGroupSettings name="ItemBased" indexFileNamePrefix="S" indexFolderLocation="C:\TFS\Mainline\Business.Utility.SendToTest\Business.Utility.SendToTest" imageFolderLocation="C:\TFS\Mainline\Business.Utility.SendToTest\Business.Utility.SendToTest">
            <topicEntries>
              <topicEntry topicIDSuffix="DATE" addDuplicateVSAMEntry="true">
                <itemSubFields>
                  <subField index="0" typeName="Date"/>
                </itemSubFields>
              </topicEntry>
              <topicEntry topicIDSuffix="Item" addDuplicateVSAMEntry="true">
                <itemSubFields>
                  <subField index="0" typeName="ItemNumber"/>
                </itemSubFields>
              </topicEntry>
              <topicEntry topicIDSuffix="DESCR">
                <itemSubFields>
                  <subField index="0" typeName="LongDescription"/>
                </itemSubFields>
              </topicEntry>
            </topicEntries>
          </appGroupSettings>
      </appGroups>
    </sendToTestIndexConfig>
  </SendToTestConfig>
...
</configuration>

我已经尝试了以下所有方法,但前几个 config(我认为这是有道理的)我一直得到 null,其余的都得到上述异常。

//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");

我猜这与我的命名有关。 ConfigurationManager.AppSettings 工作正常,所以我知道我有正确的 web.config。

【问题讨论】:

    标签: configuration web-config configurationsection custom-configuration


    【解决方案1】:

    实际上,在MSDN 上的GenericEnumConverter 示例中简洁地说明了问题的解决方案。我做了你可能做过的同样的事情,并在我的配置部分属性之一上明确设置了 [TypeConverter(typeof(GenericEnumConverter))] 属性并得到了同样的错误。根据上面链接的GenericEnumConverter 的文档,您无需设置此属性即可使用GenericEnumConverter 类型转换器——它由框架隐式调用。从您的配置属性规范中删除该属性,此错误应该会消失,一切都应该正常工作。

    以下是使用Enum 的配置节属性示例:

        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,它就开始工作了。
    【解决方案2】:

    我不确定这是否是最佳解决方案,但我能够解决这个问题。错误是因为我使用GenericEnumTypeConverter 类将配置字符串转换为AppGroupSubFieldTypes 枚举。我创建了自己的自定义 TypeConverters,它解决了这个问题。显然,GenericEnumTypeConverter 没有无参数构造函数,需要构造函数的枚举类型。很想知道是否有办法使用GenericEnumTypeConverter,但这对我有用。

    这让我知道了答案:Configuration Error With Custom Behaviour

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      • 2016-04-25
      • 2018-02-11
      • 1970-01-01
      相关资源
      最近更新 更多