views:

2150

answers:

4

I have a class like this:

public class RxNormFolderMgr
{
    // properties
    public string RxNormFolder { get { return ConfigurationSettings.AppSettings["rootFolder"].ToString(); } }
}

When I try to use it like this:

public class TestRxNormFolderManager : ColumnFixture
{
    public string RxNormFolder()
    {
        RxNormFolderMgr folderMgr = new RxNormFolderMgr();
        return folderMgr.RxNormFolder;
    }
}

I get an error: "System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object." The AllKeys property for AppSettings is an array of zero length where I am expecting length of 1.

My app.config file in the project looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="rootFolder" value ="C:\RxNorm" />
        <!-- Root folder must not end with slash. -->
    </appSettings>
</configuration>

I know ConfigurationSettings.AppSettings is supposed to be obsolete and I should use ConfigurationManager.AppSettings, but I can't even get that to compile. I do have a reference in the project to System.configuration (c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll on my machine) and using statement at top of my code.

I am using Fitnesse to test the code, and that's when I get the error. It's my understanding that I should also place a copy of the app.config file in the Bin>Debug folder of the test fixtures project which I have done. So, I don't know why I'm getting this error still.

Please, help.

+2  A: 

Also: try using the ConfigurationManager class instead of "ConfigurationSettings":

Use a check for NOT NULL first:

public class RxNormFolderMgr
{
    // properties
    public string RxNormFolder 
    { 
       get
       { 
           if(ConfigurationManager.AppSettings["rootFolder"] != null)
           {
               return ConfigurationManager.AppSettings["rootFolder"].ToString(); 
           }
           return string.Empty;
       }
    }
}

Is this inside a class library assembly? Those never use their own app.config - but instead the use the host app's app.config (the app that uses the class library).

Marc

marc_s
Thanks for the feedback, Marc. The RxNormFolderMgr class is inside a Service App. I can see how wrapping the code in a null test will prevent the error, but I really need to figure out why AppSettings is empty when it should contain a value for the rootFolder key.
Marvin
+1  A: 

When you are testing with FitNesse, the actual executable running is "FitServer.exe" so AppSettings is looking for a "FitServer.exe.config" in the directory with FitServer.exe lives. So a quick and dirty solution is to copy your app.config there and rename it.

A better solution is to specify the app config as described here: http://www.syterra.com/FitnesseDotNet/ApplicationConfigurationFile.html

or if you're using fitSharp (which is an enhancement of FitNesse.NET): http://www.syterra.com/Fit/AppConfigFiles.html

Mike Stockdale
A: 

Do not put it in appsettings. Use <connectionStrings>

example:

<appSettings/>

<connectionStrings> <add name="NORTHWNDConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

</connectionStrings>

string cnstr = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ToString();

TazEngine
A: 

Had the same problem, and indeed turned out I was using app.config inside a class library.

Doigen

related questions