【问题标题】:Is it possible to define <configSections> in a dependent dll's application config是否可以在依赖 dll 的应用程序配置中定义 <configSections>
【发布时间】:2010-10-01 14:05:15
【问题描述】:

我有一个应用程序的自定义 .NET 插件,我正在尝试为插件的配置文件创建 configSections。问题是如果使用 OpenMapperExeConfiguration/OpenExeConfiguration 加载配置,我无法阅读该部分。

这是我的配置文件(MyTest.dll.config)

<configuration>
  <configSections>
    <section name="test" type="MyTest, Test.ConfigRead"/>
    </configSections>
    <test>
            ..Stuff here
        </test>
    <appSettings>
        <add key="uri" value="www.cnn.com"/>
    </appSettings>
</configuration>

这是我访问测试配置部分的代码示例

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();  
fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + "config";    
Configuration applicationConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap,ConfigurationUserLevel.None);
//Using OpenExeConfiguration doesnt help either.
//Configuration applicationConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
//Accessing test section
applicationConfig.GetSection("test");

//Accessing AppSettings works fine.
AppSettingsSection appSettings = (AppSettingsSection)applicationConfig.GetSection("appSettings");
appSettings.Settings["uri"].Value;

如图所示,appsettings 的值可以正常读取。除了主应用程序的配置文件之外,是否可以在任何其他配置中包含 configSections?

【问题讨论】:

    标签: c# configuration configsection


    【解决方案1】:

    配置设置适用于应用程序(app.config 位于应用程序的根目录中,用于 .EXE,Web 根目录用于 Web 应用程序)和机器(machine.config 位于 [系统根目录]\Microsoft.NET\Framework[CLR 版本]\配置)级别。

    唯一使用的其他配置文件是策略配置文件,它用于创建程序集版本控制策略并通过使用 AL 工具链接到程序集。这显然是你不想做的。

    尝试将插件的配置部分合并到当前应用程序的配置部分中,以创建一个应用级别的配置文件,或者将它们放在 machine.config 文件中。

    【讨论】:

      【解决方案2】:

      您是否缺少“。”分隔符?

      fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + "config"; 
      

      添加'.':

      fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config"; 
      

      【讨论】:

        【解决方案3】:

        这不像你在问题中提到的那样工作。

        您可能会觉得您能够加载 DLL.Config 文件,但应用程序并未加载该文件,但它可能工作正常,因为您在应用程序 app.config 中有相同的 appsettings 部分。默认情况下,每个 appdomain 都有一个配置文件,并且大多以 exe 命名(因此名称将是 applicationname.exe.config

        默认情况下,这是 .net 框架加载的用于读取配置的文件。 因此,我不建议维护 .dll.config 文件

        现在您有两种选择来实现您想要实现的目标:

        选项 1:您可以为每个 ConfigurationSection 维护单独的配置文件

        从 ConfigurationSection 继承的每个类都有一个名为“configSource”的属性。在 main application.exe.config 中,您可以指定一个自定义部分,如下所示:

        <CustomSection configSource="{relative file name}" />
        <appSettings file = "relative file name" />
        

        通过这种方式,您可以将配置部分拆分为多个配置文件,并且您仍然可以使用常规 system.configuration 语法访问它们。

        更多详情请参考this

        选项 2:更改默认 exe

        默认配置文件名为 application.exe.config。 可以使用以下语法进行更改

        AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
        

        这样您可以将任何其他 xml 文件设置为程序的配置文件。
        请注意,您必须在首次调用配置类之前调用​​此 SetData 方法(即在系统读取配置文件之前)。您可以将 .dll.config 设置为应用程序配置文件,并且可以从那里读取所有配置部分。有关选项 2 的更多详细信息,请参阅 this

        希望这能提供足够的信息。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-22
          • 1970-01-01
          • 2016-04-10
          • 1970-01-01
          • 2022-01-15
          • 2011-04-29
          相关资源
          最近更新 更多