2016-11-25 9 views
0

我目前正在開發用於開發可擴展Windows服務的模板解決方案項目。在Connections.config中找不到NUnit的ConnectionString

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

我也有同樣的解決方案中兩個獨立的C#庫項目(ServiceTemplateDAL和ServiceTemplateLibrary),其包含我的模型和DAL以及大部分工作實際完成的地方。

我在我的解決方案中創建了一個名爲ServiceTemplateUnitTesting的第四個項目,我正在使用它來編寫一套NUnit的單元測試。我的問題是NUnit無法找到我的Connections.config文件。

我做了一些Google搜索,並知道您必須將配置文件重命名爲程序集的名稱。所以,我嘗試複製ServiceTemplate.exe.config並將其放在名爲ServiceTemplateUnitTesting.dll.config的ServiceTemplateUnitTesting項目的bin/Debug /中,然後運行測試,但仍無法讀取connections.config文件。

當NUnit以這種方式分離時,NUnit可以使用多個.config文件嗎?分離的原因是,我沒有源代碼管理中密碼和敏感數據的連接字符串列表。

謝謝!

回答

0

我設法弄清楚了我自己的問題。在我的情況下,我有兩個.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文件。

向解決方案添加後期構建選項可能是一個好主意,它將xcopy配置文件從您的啓動應用程序中複製並重命名爲測試程序集,並將其放置在測試程序集的/ bin/debug中。

0

NUnit WRT配置文件設計背後的思想是,測試程序集的配置文件不會總是與您的應用程序的配置文件匹配。

因此,nunit將您的測試程序集AppDomain的配置文件設置爲您的測試dll +「.config」的名稱。 NUnit對此配置文件不做任何處理 - 它不會讀取它 - 所以NUnit不可能以某種方式跟隨第二個配置文件的鏈接。只有.NET運行時和測試程序集本身知道或可以訪問此配置。

相關問題