5

次の例外が発生します。

私の設定ファイルは次のようになります

<configSections>
    <sectionGroup name="point.System">
        <section name="singleInstanceCache"
            type="xyz.Point.System.Configuration.SingleInstanceCache, Point.System" />
    </sectionGroup>
    <sectionGroup name="point.Services">
        <sectionGroup name="xServices" type="xyz.Point.Messaging.PointServiceConfiguration.PointServices, Barcap.FIA.Point.Messaging">
            <section name="xService"
                type="xyz.Point.Messaging.PointServiceConfiguration.PointService, Barcap.FIA.Point.Messaging" />
        </sectionGroup>
    </sectionGroup>
</configSections>

<point.Services>
    <xServices>
        <xService name="Service1" type="IService" >
            <endpoints>
                <endpoint aliasName="incoming" endpointName="Subscriber"/>
                <endpoint aliasName="outgoing" endpointName="Publisher"/>
            </endpoints>
        </xService>
        <xService name="BlobService" type="IPortfolioService" >
            <endpoints>
                <endpoint aliasName="incoming" endpointName="Subscriber"/>
                <endpoint aliasName="outgoing" endpointName="Publisher"/>
            </endpoints>
        </xService>
    </xServices>
</point.Services>

ここに私がそれをロードするコードがあります:

public class PointServices : ConfigurationSection
{
    public static PointServices Get()
    {
        var t = (PointServices)ConfigurationManager.GetSection("point.Services/xServices");

        return null;
    }

    //<summary>
    //Declares a collection element represented in the following configuration sub-section
    //<singleInstances> <add .../> </singleInstances> 
    //</summary>
    [ConfigurationProperty("xServices", IsDefaultCollection = true)]
    [ConfigurationCollection(typeof(PointServices), AddItemName = "xService")]
    public PointServicesCollection Services
    {
        get { return (PointServicesCollection) base["xServices"]; }
    }
}

public class PointService : ConfigurationElement
{
    [ConfigurationProperty("name",IsRequired = true)]
    public string Name
    {
        get { return this["name"].ToString(); }
    }

    [ConfigurationProperty("type", IsRequired = true)]
    public string Type
    {
        get { return this["type"].ToString(); }
    }

    [ConfigurationProperty("endpoints", IsRequired = false)]
    [ConfigurationCollection(typeof(EndpointAliasCollection), AddItemName = "endpoint")]
    public EndpointAliasCollection Endpoints
    {
        get { return (EndpointAliasCollection)this["endpoints"]; }
    }
}

このエラーが発生する理由がわかっている場合は、それが役に立ちます。

ありがとう

4

2 に答える 2

14

セクション グループをコレクションとして、セクションをコレクション内のアイテムとして使用しようとしていますが、これは意図したものではないため、エラーが発生します。

基本的に、他のセクションを含める必要がないため、point.Services をセクションとして定義するだけで済みます。次に、コレクション プロパティを定義して構成要素を含めます。次のようにコードを更新できます。

構成:

<configSections>
    <section name="point.Services" 
        type="xyz.Point.Messaging.PointServiceConfiguration.PointServices, Barcap.FIA.Point.Messaging" />
</configSections>

<point.Services>
    <xServices>
        <xService name="Service1" type="IService" >
            <endpoints>
                <endpoint aliasName="incoming" endpointName="Subscriber"/>
                <endpoint aliasName="outgoing" endpointName="Publisher"/>
            </endpoints>
        </xService>
        <xService name="BlobService" type="IPortfolioService" >
            <endpoints>
                <endpoint aliasName="incoming" endpointName="Subscriber"/>
                <endpoint aliasName="outgoing" endpointName="Publisher"/>
            </endpoints>
        </xService>
    </xServices>
</point.Services>

コードは次のとおりです。

public class PointServices : ConfigurationSection
{
    public static PointServices Get()
    {
        return (PointServices) ConfigurationManager.GetSection("point.Services");
    }

    [ConfigurationProperty("xServices", IsDefaultCollection = true)]
    [ConfigurationCollection(typeof(PointService), AddItemName = "xService")]
    public PointServicesCollection Services
    {
        get { return (PointServicesCollection) base["xServices"]; }
    }
}

public class PointService : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get { return this["name"].ToString(); }
    }

    [ConfigurationProperty("type", IsRequired = true)]
    public string Type
    {
        get { return this["type"].ToString(); }
    }

    [ConfigurationProperty("endpoints", IsRequired = false)]
    [ConfigurationCollection(typeof(EndpointAlias), AddItemName = "endpoint")]
    public EndpointAliasCollection Endpoints
    {
        get { return (EndpointAliasCollection) this["endpoints"]; }
    }
}

それを分解するには

  • PointServices は <point.Services> セクションにマップされる構成セクションであるため、静的 Get() メソッドはこれを反映します
  • PointServices は、項目タイプが PointService (PointServices ではなく) であり、要素名 xService にマップされる PointServiceCollection であるコレクション プロパティ Services を定義します。
  • PointService 要素は要素であり、セクションではありません。ここでも、コレクション プロパティ属性はコンテナー タイプではなくアイテム タイプを定義することに注意してください。

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

于 2009-11-20T05:43:56.443 に答える
-1

Point.System は構成ファイルのどこにありますか? 0 != 1 (コンピューターに対して) であるため、不平を言っているだけかもしれません。

于 2009-11-20T05:21:07.830 に答える