因为我的新项目具有复杂的配置结构,所以我学习如何将类ConfigurationManager与自定义配置一起使用。
为此,我以MSDN How to: Create Custom Configuration Sections Using 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。
我认为它与配置文件的部分名称中的类型有关。但是,尝试添加完整程序集名称等不同的值并没有帮助。
唉,我无法在GetSection中找到有关字符串参数的大量信息。
那我的app.config或我的GetSection调用有什么问题?
答案 0 :(得分:0)
您可以尝试在配置中指定TryConfigManagement.PageAppearance类型所在的程序集:
<sectionGroup name="pageAppearanceGroup">
<section
name="pageAppearance"
type="TryConfigManagement.PageAppearance, TryConfigManagement"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
然后不要在GetSection参数中包含程序集名称。
此外,请删除部分名称后的空格。
答案 1 :(得分:0)
Smiech关于案例敏感性的评论有帮助(感谢Smiech!),但这不是完整的答案。
如果使用类ConfigurationSectionGroupCollection和ConfigurationSectionCollection,则可以更容易地阅读和维护,而不是使用&#34; 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>
部分定义的字段:
现在我需要做的就是找到一个摆脱所有魔法字符串的方法。但那是另一回事。