2009-11-20 5 views
5

ich die folgende Ausnahme erhalten: „Abschnitte müssen nur einmal pro Konfigurationsdatei erscheint das Hilfethema für Ausnahmen Siehe..“Sektionen müssen nur einmal pro Konfigurationsdatei erscheinen! Warum?

Blick meine Konfigurationsdatei wie diese:

<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> 

hier ist der Code, wo ich es laden:

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"]; } 
    } 
} 

wenn Sie haben keine Ahnung, warum ich diese äh bin immer ror, das wäre hilfreich.

Danke

Antwort

13

Sie versuchen, eine Abschnittsgruppe als eine Sammlung und Abschnitte als Elemente in der Sammlung zu verwenden, für die sie nicht vorgesehen sind, daher die Error.

Grundsätzlich müssen Sie point.Services nur als Abschnitt definieren, da er keine anderen Abschnitte enthalten muss. Definieren Sie dann eine Auflistungseigenschaft, die Konfigurationselemente enthält. Sie können den Code wie folgt aktualisieren:

Config:

<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> 

Dann wird der Code:

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"]; } 
    } 
} 

Um es zu brechen:

  • PointServices ist ein Konfigurationsabschnitt, der dem Bereich < point.Services zugeordnet ist, so dass die statische Methode Get() dies widerspiegelt
  • PointServices definiert eine Auflistungseigenschaft Services, die eine PointServiceCollection mit dem Elementtyp PointService (NOT PointServices) ist Xservice
  • PointService Elemente Elemente abgebildet dem Elementnamen sind und nicht die Abschnitte, beachten Sie wieder, dass die Sammlung Eigenschaft Attribut den Elementtyp definiert, nicht den Containertyp

Hoffnung, das hilft.

+0

Brilliante Lösung, genau was ich brauche. Ich würde nur die Tatsache hinzufügen, dass die PointServicesCollection-Klasse von ConfigurationElementCollection erben muss. [Dieser Beitrag] (http://stackoverflow.com/questions/7380627/unrecognized-element-exception-with-custom-c-sharp-config) hat ebenfalls geholfen. – army

-1

Wo ist Point.System in der Konfigurationsdatei? Es könnte sich nur beschweren, weil 0! = 1 (zum Computer)

+0

nein, das point.system hat nichts damit zu tun. dieser Teil funktioniert gut. es bezogen sich auf point.services –

Verwandte Themen