在 Connections.config 中找不到 NUnit 的 ConnectionString

Cannot Find ConnectionString for NUnit in Connections.config

我目前正在开发一个用于开发可扩展 windows 服务的模板解决方案项目。

在我的 Windows 服务 C# 项目 (ServiceTemplate) 中,我有一个 App.config 文件,其中包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
      <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
    <connectionStrings configSource="Connections.config"/>

    <!-- NLog Settings -->
    <nlog>
      <targets>
        <target name="programlog" type="File" fileName="${basedir}/Logs/program-logs/program-log${shortdate}.log" layout="${logger} - [${longdate}] [${level}] -> ${message} (${callsite} @ line ${callsite-linenumber})" />
        <target name="servicelog" type="File" fileName="${basedir}/Logs/service-logs/service-log${shortdate}.log" layout="${logger} - [${longdate}] [${level}] -> ${message} (${callsite} @ line ${callsite-linenumber})" />
        <target name="tasklog" type="File" fileName="${basedir}/Logs/task-logs/task-log${shortdate}.log" layout="${logger} - [${longdate}] [${level}] -> ${message} (${callsite} @ line ${callsite-linenumber})" />
        <target name="checkinlog" type="File" fileName="${basedir}/Logs/checkin-logs/checkin-log${shortdate}.log" layout="${logger} - [${longdate}] [${level}] -> ${message} (${callsite} @ line ${callsite-linenumber})" />
        <target name="databaselog" type="File" fileName="${basedir}/Logs/database-logs/database-log${shortdate}.log" layout="${logger} - [${longdate}] [${level}] -> ${message} (${callsite} @ line ${callsite-linenumber})" />
        <target name="unittestlog" type="File" fileName="${basedir}/Logs/unittest-logs/unittest-log${shortdate}.log" layout="${logger} - [${longdate}] [${level}] -> ${message} (${callsite} @ line ${callsite-linenumber})" />
      </targets>
      <rules>
        <logger name="ProgramLog" writeTo="programlog"/>
        <logger name="ServiceLog" writeTo="servicelog"/>
        <logger name="TaskLog" writeTo="tasklog"/>
        <logger name="CheckInLog" writeTo="checkinlog"/>
        <logger name="DatabaseLog" writeTo="databaselog"/>
        <logger name="UnitTestLog" writeTo="unittestlog" />
      </rules>
    </nlog>
 </configuration>

我在包含我的模型和 DAL 的同一个解决方案中还有两个单独的 C# 库项目(ServiceTemplateDAL 和 ServiceTemplateLibrary),大部分工作实际上是在其中完成的。

我在名为 ServiceTemplateUnitTesting 的解决方案中创建了第四个项目,我用它来编写一套带有 NUnit 的单元测试。我的问题是 NUnit 找不到我的 Connections.config 文件。

我进行了一些谷歌搜索,知道您必须将配置文件重命名为程序集的名称。因此,我尝试复制 ServiceTemplate.exe.config 并将其放置在名为 ServiceTemplateUnitTesting.dll.config 和 运行 的 ServiceTemplateUnitTesting 项目的 bin/Debug/ 中,但它仍然无法读取 connections.config 文件.

当 NUnit 以这种方式分隔时,是否可以使用多个 .config 文件?分开的原因是我在源代码管理中没有带有密码和敏感数据的连接字符串列表。

谢谢!

NUnit WRT 配置文件的设计背后的想法是,您的测试程序集的配置文件并不总是与您的应用程序的配置文件相匹配。

因此,nunit 将测试程序集 AppDomain 的配置文件设置为测试 dll 的名称 +“.config”。 NUnit 对这个配置文件不做任何事情——它不读取它——所以 NUnit 不可能以某种方式跟随 link 到第二个配置文件。只有 .NET 运行时和您的测试程序集本身知道或可以访问此配置。

我自己设法解决了这个问题。就我而言,我有两个 .config 文件;在我的 windows 服务项目中,一个名为 App.config,另一个名为 Connections.config。

我的连接字符串隐藏在 Connections.config 中,以确保它们不包含在源代码管理中但仍可由应用程序访问。

解决方案是从 'ServiceTemplate' 项目中取出 App.config 文件并将其重命名为 'ServiceTemplateUnitTesting.dll.config' 并将其放置在 'ServiceTemplateUnitTesting' 项目的 /bin/debug 中文件夹。与此同时,我还在 'ServiceTemplateUnitTesting' 的 /bin/debug 文件夹中放置了 Connections.config 文件的副本。在此之后,加载的 NUnit 测试 dll 能够读取我原来的 App.config 文件。

向解决方案添加 post 构建选项可能是个好主意,以便从您的启动应用程序 xcopy 配置文件并将其重命名为测试程序集并将其放置在测试程序集的 /bin/debug.