views:

5282

answers:

6

I'm trying to finish this exception handler:

if (ConfigurationManager.ConnectionStrings["ConnectionString"]==null)
{
    string pathOfActiveConfigFile = ...?
    throw new ConfigurationErrorsException(
       "You either forgot to set the connection string, or " +
       "you're using a unit test framework that looks for  "+
       "the config file in strage places, update this file : " 
       + pathOfActiveConfigFile);
}

This problem seems to only happen to me when I'm using nUnit.

+2  A: 

If you mean you are only getting a null return when you use NUnit, then you probably need to copy the ConnectionString value the your app.config of your application to the app.config of your test library.

When it is run by the test loader, the test assembly is loaded at runtime and will look in its own app.config (renamed to testAssembly.dll.config at compile time) rather then your applications config file.

To get the location of the assembly you're running, try

System.Reflection.Assembly.GetExecutingAssembly().Location
Colin Desmond
A: 

Depending on the location of your config file System.Reflection.Assembly.GetExecutingAssembly().Location might do what you need.

William Edmondson
Additionally, you can have several "active" config files at the same time. Machine.config, framework level web.config, app level config, etc... so I don't think there is a way to automatically locate a unique connection string file location without parsing through all the possible config files available to your app.
William Edmondson
+22  A: 

Try this

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

Hope it helps

Cédric Rup
Did the job for me - thanks! :o)
John Sibly
+2  A: 

Make sure you click the properties on the file and set it to "copy always" or it will not be in the Debug\ folder with your happy lil dll's to configure where it needs to be and add more cowbell

Chad Grant
+3  A: 

Strictly speaking, there is no single configuration file (for non-ASP.NET). There can be three with the inbuilt (System.Configuration) support (in addition to the machine config).

To get the "global" configuration (exe.config):

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
                    .FilePath

Use different ConfigurationUserLevel values for per-use roaming and non-roaming configuration files.

Richard
I first upped the "Answer", which was very helpful.But at the end I really needed this approach, so thanks for putting it up here as well.
Noam Gal
+1  A: 

The first time I realized that the Unit testing project referenced the app.config in that project rather then the app.config associated with my production code project (off course, DOH) I just added a line in the Post Build Event of the Prod project that will copy the app.config to the bin folder of the test project.

Problem solved

I haven't noticed any weird side effects so far, but I am not sure that this is the right solution, but at least it seems to work.

Kasper