[英]ConfigurationManager.GetSection(“sectionName”) throws ConfigurationErrorsException for custom section
[英]ConfigurationManager.GetSection doesn't return custom section
因為我的新項目具有復雜的配置結構,所以我正在學習如何將類ConfigurationManager與自定義配置一起使用。
為此,我以MSDN如何:以ConfigurationSection為例創建自定義配置節 。 此示例顯示了一個類的配置部分,該部分聚集了其他可配置類。
MyProblem:ConfigurationManager.GetSection不返回任何節
我創建了一個ConsoleApplication。
更改了App.Config(始終復制),如下所示:
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- Configuration section-handler declaration area. --> <configSections> <sectionGroup name="pageAppearanceGroup"> <section name="pageAppearance" type="TryConfigManagement.PageAppearance" allowLocation="true" allowDefinition="Everywhere" /> </sectionGroup> </configSections> <!-- Configuration section settings area. --> <pageAppearanceGroup> <pageAppearance remoteOnly="true"> <font name="TimeNewRoman" size="18"/> <color background="000000" foreground="FFFFFF"/> </pageAppearance> </pageAppearanceGroup> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/> </startup> </configuration>
請注意,我使用了MSDN中示例中的配置組。
我的源代碼:
namespace TryConfigManagement
{
class Program
{
static void Main(string[] args)
{
string sectionName = "pageAppearanceGroup/pageAppearance";
object section = ConfigurationManager.GetSection(sectionName);
這段代碼引發以下異常:
An error occurred creating the configuration section handler
for pageAppearanceGroup/pageAppearance:
Could not load type 'TryConfigManagement.PageAppearanceSection'
from assembly 'System.Configuration, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
(C:\...\bin\Debug\TryConfigManagement.vshost.exe.config line 6)
盡管我完全按照我從StackOverflow得出的示例中所做的那樣做,但是我應該添加程序集信息,大概如下:
class Program
{
static void Main(string[] args)
{
var assembly = Assembly.GetExecutingAssembly();
string sectionName = "pageAppearanceGroup/pageAppearance ," +
assembly.FullName;
object section = ConfigurationManager.GetSection(sectionName);
現在不再拋出異常,但是返回的值為null。
我認為這與配置文件的節名稱中的類型有關。 但是,嘗試使用其他值(例如添加完整的程序集名稱)無濟於事。
las,我在GetSection中找不到很多有關字符串參數的信息。
那么我的app.config或GetSection調用怎么了?
您可以嘗試在配置中指定TryConfigManagement.PageAppearance類型所在的程序集:
<sectionGroup name="pageAppearanceGroup">
<section
name="pageAppearance"
type="TryConfigManagement.PageAppearance, TryConfigManagement"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
然后,不要在GetSection參數中包含程序集名稱。
另外,刪除您的節名稱后的空格 。
Smiech關於區分大小寫的評論有所幫助(謝謝Smiech!),但這不是完整的答案。
如果使用類ConfigurationSectionGroupCollection和ConfigurationSectionCollection,則比起“ pageAppearanceGroup / pageAppearance”這樣的魔術字符串處理,讀取和維護起來要容易得多。
該代碼如下所示:
namespace MyNameSpace
{
class Program
{
static void Main(string[] args)
{
const string configGroupName = "pageAppearanceGroup";
const string configSectionName = "pageAppearance";
Configuration config = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
ConfigurationSectionGroup configGroup = config.SectionGroups[configGroupName];
PageAppearanceSection pageAppearanceConfig = (PageAppearanceSection)configGroup.Sections[configSectionName];
在這些之后,我可以執行以下語句:
Console.WriteLine("PageAppearance property values:");
Console.WriteLine("RemoteOnly = " + pageAppearanceConfig.RemoteOnly);
Console.WriteLine("Font: Name = {0}, Size = {1}",
pageAppearanceConfig.Font.Name,
pageAppearanceConfig.Font.Size);
Console.WriteLine("Color: Foreground {0}, Background {1}",
pageAppearanceConfig.Color.Foreground,
pageAppearanceConfig.Color.Background);
注意:為了了解App.Config,我將名稱空間更改為MyNameSpace。 程序集名稱仍為TryConfigManagement
為了完整起見,App.Config的內容。 請注意在何處使用名稱空間和程序集名稱
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- Configuration section-handler declaration area. -->
<configSections>
<sectionGroup name ="pageAppearanceGroup">
<section name="pageAppearance"
type ="TryConfigManagement.PageAppearanceSection, TryConfigManagement"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
</configSections>
<!-- Configuration section settings area. -->
<pageAppearanceGroup>
<pageAppearance>
<font name="TimeNewRoman" size="18"/>
<color background="000000" foreground="FFFFFF"/>
</pageAppearance>
</pageAppearanceGroup>
</configuration>
部分定義的字段:
現在,如果找到一種擺脫所有魔術弦的方法,我所要做的就是。 但這是另一回事。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.