Aaron G Aaron G - 7 days ago 5
C# Question

Cannot Find ConnectionString for NUnit in Connections.config

I am currently working on a template solution project for developing extendable windows services.

Within my Windows Service C# project (ServiceTemplate) I have an App.config file that contains the following:

<?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>


I also have two separate C# library projects (ServiceTemplateDAL and ServiceTemplateLibrary) within the same solution that contains my models and DAL and where most of the work is actually done.

I have created a fourth project within my solution called ServiceTemplateUnitTesting which I am using to write a suite of unit tests with NUnit. My issue is NUnit cannot find my Connections.config file.

I did some googling and know that you have to rename the config file to the name of the assembly. So, I tried copying the ServiceTemplate.exe.config and placing it in bin/Debug/ of the ServiceTemplateUnitTesting project called ServiceTemplateUnitTesting.dll.config and running the tests but it is still not being able to read the connections.config file.

Is it possible for NUnit to use multiple .config files when they are separated this way? The reason for the separation is so I do not have a list of connection strings with passwords and sensitive data in source control.

Thanks!

Answer

I managed to figure out the issue on my own. In my case, I had two .config files; both in my windows service project, one called App.config and the other called Connections.config.

My connection strings were hidden away in Connections.config in order to make sure they were not included in source control but still accessible by the application.

The solution was to take the App.config file from the 'ServiceTemplate' project and rename it to 'ServiceTemplateUnitTesting.dll.config' and placing it in the 'ServiceTemplateUnitTesting' project's /bin/debug folder. Along with this, I also placed a copy of the Connections.config file in the /bin/debug folder of 'ServiceTemplateUnitTesting'. After this, the loaded NUnit test dll was able to read my original App.config file.

It may be a good idea to add a post build option to the solution to xcopy the config file from your starting application and renaming it to the test assembly and placing it within the test assembly's /bin/debug.

Comments