将 App.config 与在两个不同项目之间共享的自定义部分一起使用

Use App.config with a Custom Section shared between two different projects

场景

我开发了一个 windows 服务,它由其 App.config 文件配置。 此文件包含标准部分(connectionStringsappSettings)和自定义部分(sourceTabSection[=62=)中的信息]).
在 windows 服务项目中,我有 4 个 class 允许我 get/set 配置文件内容。它们基于本文中所写的内容:Writing a Custom ConfigurationSection to handle a Collection 并且我在我的服务中使用它们没有任何问题。

当我尝试 get/set App.config 属于 Windows 服务的自定义部分(使用标准部分我没有任何问题)时,问题就来了,使用另一个应用程序在我的例子中是一个 Windows 表单,允许用户为 windows 服务设置 view/set 参数。
Windows 表单应用程序与服务使用相同的 4 个 classes 包,以处理 App.config。 当 Windows 服务的 get/set 自定义参数的代码在 Windows 表单应用程序上执行时,我收到以下错误消息:

{"An error occurred creating the configuration section handler for sourceTabSection: Could not load type 'DataReportingService.CustomSourceTabSection.SourceTabSection' from assembly 'DataReportingService'."}

问题是由于 App.config

中的以下代码行引起的

<section name="sourceTabSection" type="DataReportingService.CustomSourceTabSection.SourceTabSection, DataReportingService"/>

上面显示的标签的属性类型具有以下含义(这里解释为:section Element for configSections):

type="Fully qualified class name, assembly file name, version, culture, public key token"

根据 Writing a Custom ConfigurationSection to handle a Collection 文章中的内容,我只定义了前两个参数(完全限定的 class 名称,程序集文件名)的属性类型。 Microsoft 文档(不再维护)没有指定不能定义其他参数,但我遵循的示例和其他人使用了这种方法。 然而,重点是微软文档中关于类型属性的短语:

The assembly file must be located in the same application directory

因此,由于这种联系,似乎无法使用此方法从另一个应用程序 B(具有另一个程序集)处理应用程序 A 的自定义部分。

那么你知道我该如何解决这个问题吗?

Windows 服务 - App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="sourceTabSection" type="DataReportingService.CustomSourceTabSection.SourceTabSection, DataReportingService"/>  
  </configSections>

  <!-- *** CUSTOM SECTION *** -->
  <sourceTabSection>
    <Tables>
      <sourceTab name="TEST" db_conn_str="****"
        keep_time="1" scan_frequency_process_rows="1" 
        scan_frequency_delete_processed_rows="1" />
      <sourceTab name="TEST_2" db_conn_str="****"
        keep_time="1" scan_frequency_process_rows="1" 
        scan_frequency_delete_processed_rows="1" />
    </Tables>
  </sourceTabSection>

  <!-- *** STANDARD SECTIONS *** -->

  <connectionStrings>
    <add name="DB_Target" connectionString="Data Source=192.168.2.2;Initial Catalog=PlantDompe;Persist Security Info=True;User ID=sa;Password=Gf6swML0MXiqbOFuvRDvdg==;"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

  <appSettings>
    <add key="TAB_ALARMS_TARGET" value="AlarmsProcess" />
    <add key="TAB_VALUE_TARGET" value="USER_CHANGES" />
    <add key="TAB_LOGINS_TARGET" value="USER_LOGONS" />
    <add key="LOG_DIR" value="C:/Users/rossi/Documents/Visual Studio 2017/Projects/DRS_proj/Log/" />
  </appSettings>

<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<system.web>
    <trust level="Full" />
    <webControls clientScriptsLocation="/aspnet_client/{0}/{1}/" />
</system.web>
</configuration>

丑陋的解决方案

如果通过在需要 view/set App.config 中的参数(自定义和非自定义)的 Windows 表单应用程序上执行以下两个步骤找到解决此问题的方法Windows 服务的 ]:

  1. 使用 visual studio 我转到“解决方案属性”>“应用程序”选项卡,然后更改以下值
    程序集名称 = DataReportingService
    默认命名空间 = DataReportingService

    注:DataReportingService是window服务的名称,带有App.config文件
  2. 查找所有对旧名称空间的引用并将其替换为新名称空间

通过这种方式我可以处理 App.config 的自定义部分,但老实说这是一个非常丑陋的解决方案,我认为应该有更好的东西。

感谢@Alex Paven,你的评论帮助我解决了这个问题!
下面是我所做的详细步骤:

  1. 我移动了 4 个 class 处理 Windows 服务配置文件的 Class 库项目 (.NET Framework) 调用:DRS_CustomConfig.

  2. 我用以下值更改了 4 个 classes 的命名空间:DRS_CustomConfig 然后我编译了项目。

  3. 我在 Windows 服务项目和 Windows 表单应用程序中都链接了外部库

  4. 对于需要使用外部库中包含的 class 的两个项目的每个 class,我插入了以下代码:

    using DRS_CustomConfig;
    
  5. 在 Windows 服务的 App.config 中,我将 section 元素更改如下:

        <section name="sourceTabSection" 
    type="DataReportingService.CustomSourceTabSection.SourceTabSection, 
    DataReportingService"/>

新建

        <section name="sourceTabSection" 
    type="DRS_CustomConfig.SourceTabSection, DRS_CustomConfig"/>