2017-01-23 120 views
3

我有一些要在自定义配置文件中读取的类,这些文件在我的(沙箱)MVC应用程序和控制台应用程序中工作。但是,当它从实际需要的Web窗体应用程序运行时,它将无法读取除顶层部分之外的任何内容。他们都以.NET 4.5为框架。 自定义配置是一个“运营商”部分,其中包含运营商列表,每个运营商都有自己的设置。 web.config中提取:在WebForms应用程序中读取自定义配置文件

<?xml version="1.0"?> 
<configuration> 
    <configSections> 
     <section name="CarrierSection" type="SmallWFApp.CarrierSection"/> 
    </configSections> 

的自定义配置文件部分:

那(我认为)
<?xml version="1.0"?> 
<CarrierSection> 
    <carriers> 
    <carrier name="ups" default-code="UPS0041" > 
     <pickupreq value="N"/> 
     <limits> 
     <limit name="non-doc" weight="200" length="270" width="165" height="165" square="330" /> 
     <limit name="doc" weight="2.5" length="0" width="0" height="0" square="0" /> 
     <limit name="consignment" max-eu="200" max-world="0"/> 
     </limits> 
     <services> 
     <service name="document" caption="DOCUMENT EXPRESS" code="D" live="Y"/> 
     <service name="parcel" caption="EXPRESS" code="N" live="Y" /> 
     <service name = "road" caption="ECONOMY ROAD" code="P" live="N" /> 
     </services> 
    </carrier> 
    <carrier name="dhl" default-code="DHL0014" > 
     <pickupreq value="Y"/> 
     <limits > 
     <limit name="non-doc" weight="200" length="270" width="165" height="165" square="330" /> 
     <limit name="doc" weight="2.5" length="0" width="0" height="0" square="0"/> 
     </limits> 
     <services> 
     <service name="9am" caption="NEXT DAY 9AM" code="9" /> 
     <service name = "noon" caption="NEXT DAY 12NOON" code="2" /> 
     </services> 
    </carrier> 
    </carriers> 
</CarrierSection> 

C#的配置部分类提供的强类型:

using System; 
using System.Configuration; 

namespace SmallWFApp 
{ 
public class CarrierSection : ConfigurationSection 
{ 
    [ConfigurationProperty("carriers")] 
    public CarrierElementCollection Carriers 
    { 
     get { return this["carriers"] as CarrierElementCollection; } 
    } 
} 

//----------------------------------------- 

public class CarrierElementCollection : ConfigurationElementCollection 
{ 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new CarrierElement(); 
    } 
    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((CarrierElement)element).Name; 
    } 
    public override ConfigurationElementCollectionType CollectionType 
    { 
     get { return ConfigurationElementCollectionType.BasicMap; } 
    } 
    protected override string ElementName 
    { 
     get { return "carrier"; } 
    } 
    public CarrierElement this[int index] 
    { 
     get { return (CarrierElement)BaseGet(index); } 
     set 
     { 
      if (BaseGet(index) != null) 
      { 
       BaseRemoveAt(index); 
      } 
      BaseAdd(index, value); 
     } 
    } 
    new public CarrierElement this[string employeeID] 
    { 
     get { return (CarrierElement)BaseGet(employeeID); } 
    } 
    public bool ContainsKey(string key) 
    { 
     bool result = false; 
     object[] keys = BaseGetAllKeys(); 
     foreach (object obj in keys) 
     { 
      if ((string)obj == key) 
      { 
       result = true; 
       break; 
      } 
     } 
     return result; 
    } 
} 

//----------------------------------------------------------------- 
public class CarrierElement : ConfigurationElement 
{ 
    [ConfigurationProperty("name", DefaultValue = "Other", IsRequired = true, IsKey = true)] 
    public string Name 
    { 
     get { return this["name"] as string; } 
     set { this["name"] = value; } 
    } 


    [ConfigurationProperty("default-code", IsRequired = true)] 
    public string DefaultCode 
    { 
     get { return this["default-code"] as string; } 
     set { this["default-code"] = value; } 
    } 

    [ConfigurationProperty("pickupreq")] 
    public ReqPickupConfigElement ReqPickup 
    { 
     get { return base["pickupreq"] as ReqPickupConfigElement; } 
    } 


    [ConfigurationProperty("limits")] 
    public LimitElementCollection Limits 
    { 
     get { return this["limits"] as LimitElementCollection; } 
    } 

    [ConfigurationProperty("services")] 
    public ServiceElementCollection Services 
    { 
     get { return this["services"] as ServiceElementCollection; } 
    } 
} 


//-------------------------------------------- 

public class ReqPickupConfigElement : System.Configuration.ConfigurationElement 
{ 
    [ConfigurationProperty("value", DefaultValue = "N")] 
    [StringValidator(MinLength = 1, MaxLength = 1)] 

    public string Value 
    { 
     get { return base["value"] as string; } 
    } 
} 

的代码在code-behind-aspx.cs类中读取:

try 
{ 
    CarrierSection config = 
         (CarrierSection)System.Configuration.ConfigurationManager.GetSection(
         "CarrierSection"); 

    if (config != null) 
    { 
     foreach (CarrierElement app in config.Carriers) 
     { 
      //... (never comes in here) ... 
     } 

     //lbConfigread.Text = found.ToString(); 
    } 
} 
catch (Exception ex) 
{ 
    //... 
} 

C#的配置集合/元件来读取 '限制' 的每个载波:

namespace SmallWFApp 
{ 
    public class LimitElementCollection : ConfigurationElementCollection 
    { 
     protected override ConfigurationElement CreateNewElement() 
     { 
      return new LimitElement(); 
     } 
     protected override object GetElementKey(ConfigurationElement element) 
     { 
      return ((LimitElement)element).Name; 
     } 
     public override ConfigurationElementCollectionType CollectionType 
     { 
      get { return ConfigurationElementCollectionType.BasicMap; } 
     } 
     protected override string ElementName 
     { 
      get { return "limit"; } 
     } 
     public LimitElement this[int index] 
     { 
      get { return (LimitElement)BaseGet(index); } 
      set 
      { 
       if (BaseGet(index) != null) 
       { 
        BaseRemoveAt(index); 
       } 
       BaseAdd(index, value); 
      } 
     } 
     new public LimitElement this[string employeeID] 
     { 
      get { return (LimitElement)BaseGet(employeeID); } 
     } 
     public bool ContainsKey(string key) 
     { 
      bool result = false; 
      object[] keys = BaseGetAllKeys(); 
      foreach (object obj in keys) 
      { 
       if ((string)obj == key) 
       { 
        result = true; 
        break; 
       } 
      } 
      return result; 
     } 
    } 
    public class LimitElement : ConfigurationElement 
    { 
     [ConfigurationProperty("name", IsRequired = true, IsKey = true)] 
     public string Name 
     { 
      get { return this["name"] as string; } 
      set { this["name"] = value; } 
     } 

     [ConfigurationProperty("weight", IsRequired = false)] 
     public string Weight 
     { 
      get { return this["weight"] as string; } 
      set { this["weight"] = value; } 
     } 

     [ConfigurationProperty("length", IsRequired = false)] 
     public string Length 
     { 
      get { return this["length"] as string; } 
      set { this["length"] = value; } 
     } 
     [ConfigurationProperty("width", IsRequired = false)] 
     public string Width 
     { 
      get { return this["width"] as string; } 
      set { this["width"] = value; } 
     } 
     [ConfigurationProperty("height", IsRequired = false)] 
     public string Height 
     { 
      get { return this["height"] as string; } 
      set { this["height"] = value; } 
     } 
     [ConfigurationProperty("square", IsRequired = false)] 
     public string Square 
     { 
      get { return this["square"] as string; } 
      set { this["square"] = value; } 
     } 

     // for total consignment 

     [ConfigurationProperty("max-eu", IsRequired = false)] 
     public string MaxEU 
     { 
      get { return this["max-eu"] as string; } 
      set { this["max-eu"] = value; } 
     } 
     [ConfigurationProperty("max-world", IsRequired = false)] 
     public string MaxWorld 
     { 
      get { return this["max-world"] as string; } 
      set { this["max-world"] = value; } 
     } 
    } 
} 

C#的配置集合/元件来读取每个载波的 '服务':

namespace SmallWFApp 
{ 
    public class ServiceElementCollection : ConfigurationElementCollection 
    { 
     protected override ConfigurationElement CreateNewElement() 
     { 
      return new ServiceElement(); 
     } 
     protected override object GetElementKey(ConfigurationElement element) 
     { 
      return ((ServiceElement)element).Name; 
     } 
     public override ConfigurationElementCollectionType CollectionType 
     { 
      get { return ConfigurationElementCollectionType.BasicMap; } 
     } 
     protected override string ElementName 
     { 
      get { return "service"; } 
     } 
     public ServiceElement this[int index] 
     { 
      get { return (ServiceElement)BaseGet(index); } 
      set 
      { 
       if (BaseGet(index) != null) 
       { 
        BaseRemoveAt(index); 
       } 
       BaseAdd(index, value); 
      } 
     } 
     new public ServiceElement this[string employeeID] 
     { 
      get { return (ServiceElement)BaseGet(employeeID); } 
     } 
     public bool ContainsKey(string key) 
     { 
      bool result = false; 
      object[] keys = BaseGetAllKeys(); 
      foreach (object obj in keys) 
      { 
       if ((string)obj == key) 
       { 
        result = true; 
        break; 
       } 
      } 
      return result; 
     } 
    } 
    public class ServiceElement : ConfigurationElement 
    { 
     [ConfigurationProperty("name", IsRequired = true, IsKey = true)] 
     public string Name 
     { 
      get { return this["name"] as string; } 
      set { this["name"] = value; } 
     } 

     [ConfigurationProperty("caption", IsRequired = true)] 
     public string Caption 
     { 
      get { return this["caption"] as string; } 
      set { this["caption"] = value; } 
     } 

     [ConfigurationProperty("code", IsRequired = true)] 
     public string Code 
     { 
      get { return this["code"] as string; } 
      set { this["code"] = value; } 
     } 

     [ConfigurationProperty("live", IsRequired = false)] 
     public string Live 
     { 
      get { return this["live"] as string; } 
      set { this["live"] = value; } 
     } 
    } 
} 

所以当您运行它时,没有错误,但是当您在调试器中检查变量时,载体属性的集合为空:

?config.Carriers 
Count = 0 
    AddElementName: "add" 
    ClearElementName: "clear" 
    CollectionType: BasicMap 
    Count: 0 
    CurrentConfiguration: null 
    ElementInformation: {System.Configuration.ElementInformation} 
    ElementName: "carrier" 
    ElementProperty: {System.Configuration.ConfigurationElementProperty} 
    ... 
    Properties: {System.Configuration.ConfigurationPropertyCollection} 
    ... 

如果这包含了一些明显的假通道,那么我很抱歉,但这让我分心;我需要在几天前发布此修复程序!

+0

凡被定义'limits','limit'和'services','service'?如果我在配置中删除这些节点,我会得到2个运营商。 – krlzlx

+0

感谢您的期待!为了提高可读性,我删除了它们。但是,如果我替换它们,它仍然没有返回载体集合,所以我已经在我的问题中恢复了它们,并且希望能够再看一看? –

+0

对不起,我无法重现您的问题。即使有限制和服务节点,我也获得了2个运营商。您的'CarrierSection'在Web.config或单独的配置文件中? – krlzlx

回答

2

您的Web.config文件,其中位于您的自定义配置文件parcelcarriers.config指定:

<CarrierSection configSource="parcelcarriers.config"/> 
相关问题