3

web.config からカスタム構成セクションを読み取るのに苦労しました: 私は Configuration Section Designer ( 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 タイプ、ブール publicOnly、ブール noCheck、ブール & canBeCached、RuntimeMethodHandle & ctor、ブール & bNeedSecurityCheck) で System.RuntimeType.CreateInstanceSlow (ブール publicOnly、ブール fillCache) で System.RuntimeType.CreateInstanceImpl (ブール publicOnly、ブール SkipVisibilityChecks) 、ブール型の 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>

私は次のすべてを試しましたがnull、最初のいくつかは取得し続けconfig(これは理にかなっていると思います)、残りは上記の例外を取得します。

//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 があることがわかります。

4

2 に答える 2

4

実際、問題の解決策は、GenericEnumConverterMSDN例で簡潔に述べられています。あなたがおそらく行ったのと同じことを行い[TypeConverter(typeof(GenericEnumConverter))]、構成セクションのプロパティの 1 つに属性を明示的に設定したところ、同じエラーが発生しました。上記のリンク先のドキュメントによると、型コンバーター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;

それが役立つことを願っています。

于 2012-09-22T18:35:46.137 に答える
2

これが最善の解決策かどうかはわかりませんが、問題を回避することができました。エラーは、GenericEnumTypeConverterクラスを使用して構成文字列をAppGroupおよびSubFieldTypes列挙型に変換していたためです。独自のカスタムTypeConverterを作成しましたが、問題は解決しました。どうやら、GenericEnumTypeConverterパラメーターのないコンストラクターはなく、コンストラクターの列挙型が必要です。使用する方法があるかどうか知りたいのですがGenericEnumTypeConverter、これは私にとってはうまくいきました。

これは私に答えを教えてくれました:カスタム動作による構成エラー

于 2012-03-14T18:41:21.130 に答える