繁体   English   中英

在Connections.config中找不到NUnit的ConnectionString

[英]Cannot Find ConnectionString for NUnit in Connections.config

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

在我的Windows Service 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>

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

我在解决方案中创建了一个名为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无法以某种方式跟随链接到第二个配置文件。 只有.NET运行时和您的测试程序集本身知道或可以访问此配置。

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

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

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

最好在解决方案中添加一个后构建选项,以从启动应用程序中复制配置文件,并将其重命名为测试程序集,然后将其放置在测试程序集的/ bin / debug中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM