2009-11-20 9 views
5

aşağıdaki exeption alıyorum: "Bölümler sadece yapılandırma dosyasında başına bir kez görünür istisnalar için yardım konusuna bakın gerekir."Bölümler her yapılandırma dosyası için sadece bir kez görünmelidir! niye ya?

bu gibi benim yapılandırma dosyası görünüm:

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

burada ben o yük kodudur:

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

Eğer ben bu er alıyorum neden bir fikrin varsa ror, bu yararlı olur.

Teşekkür

cevap

13

Koleksiyon olarak bir bölüm grubu kullanmaya ve koleksiyonda öğeler olarak bölümler olarak kullanmaya çalışıyorsunuz; hata.

Temel olarak, yalnızca başka bir bölüm içermesi gerekmediğinden, bir yapılandırma öğesi içerecek bir koleksiyon özelliği tanımladığından, yalnızca bir nokta olarak bölümleri tanımlamanız gerekir.

Config'i:

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

Ardından kod geçerli:

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

aşağı kırmak için: aşağıdaki gibi kod güncelleyebilirsiniz

  • PointServices, < point.Services> bölümüne eşlenen bir yapılandırma bölümüdür, bu nedenle statik Get() yöntemi bu
  • 'u yansıtır. PointServices, PointServiceCollection öğesi olan PointServiceCollection öğesini tanımlar ve PointService (NOT PointServices) öğesidir. xService
  • PointService elemanları elemanları değil bölümlerdir eleman adıyla eşleştirilmiş, tekrar toplama özelliği nitelik öğe türü değil, konteyner tipi yardımcı

Umut tanımlar unutmayın.

+0

Parlak çözüm, tam olarak ne ben. Sadece PointServicesCollection sınıfının ConfigurationElementCollection öğesinden devralması gereken gerçeği eklerim. [Bu yazı] (http://stackoverflow.com/questions/7380627/unrecognized-element-exception-with-custom-c-sharp-config) de yardımcı oldu. – army

-1

Point.System yapılandırma dosyasında mı? Şüpheli olabilir çünkü 0! = 1 (bilgisayara)

+0

no, point.system ile ilgili hiçbir şey yok. bu bölüm iyi çalışıyor. Bu point.services ile ilgili –

İlgili konular