2009-11-19 39 views
0

我想建立一個自定義的配置,並有一些原因,我無法得到它的工作。如果有人能看到我的問題在哪裏,我將不勝感激。ConfigurationSection - 自定義部分未定義 - 爲什麼?

下面是代碼:

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

      return t; 
     } 

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


    } 

這裏是配置:

 <sectionGroup name="point.Services"> 
      <section name="xServices" type="XYZ.Messaging.PointServiceConfiguration.PointServices, XYZ.Point.Messaging" /> 
     </sectionGroup> 
... 
    <point.Services> 
     <xServices> 
      <xService> 
      <add name="XYZService" type="XYZService" /> 
      </xService> 
     </xServices> 
     </point.Services> 

當我運行:PointServices.Get(),我越來越:

無法識別的元素'Xservice的'。

我試圖Xservice的添加到段定義如下: <section name="xService" type="XYZPoint.Messaging.PointServiceConfiguration.PointService, Barcap.FIA.Point.Messaging" />,但它似乎並沒有幫助。

如果有人有任何想法,請幫助!謝謝

回答

1

而且你需要另一個說明符Xservice的

<sectionGroup name="point.Services">   
    <sectionGroup name="xServices">   
    <section name="xService" 
     type="XYZ.Messaging.PointServiceConfiguration.PointServices, XYZ.Point.Messaging" /> 
    </sectionGroup name="xServices">   
</sectionGroup> 
+0

偉大的,它有助於解決一個問題,但現在我有這個問題: http://stackoverflow.com/questions/1768362/sections-must-only-appear-once-per-config-file-why – 2009-11-20 04:35:40

0

xServices應該是一個sectionGroup,而不是一個部分。而xService應該被定義爲一個部分。

+0

感謝您的幫助。你知道我得到這個: http://stackoverflow.com/questions/1768362/sections-must-only-appear-once-per-config-file-why – 2009-11-20 04:36:11