2

我有这样的课:

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

当我尝试像这样使用它时:

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

我收到一个错误:“System.Reflection.TargetInvocationException:调用的目标已引发异常。---> System.NullReferenceException:对象引用未设置为对象的实例。” AppSettings 的 AllKeys 属性是一个长度为零的数组,我期望长度为 1。

我在项目中的 app.config 文件如下所示:

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

我知道 ConfigurationSettings.AppSettings 应该已经过时了,我应该使用 ConfigurationManager.AppSettings,但我什至无法编译它。我在项目中确实引用了 System.configuration(我的机器上的 c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll)和 using 代码顶部的语句。

我正在使用 Fitnesse 测试代码,这就是我收到错误的时候。我的理解是,我还应该将 app.config 文件的副本放在我已经完成的测试夹具项目的 Bin>Debug 文件夹中。所以,我不知道为什么我仍然收到这个错误。

请帮忙。

4

3 回答 3

7

另外:尝试使用ConfigurationManager类而不是“ConfigurationSettings”:

首先检查 NOT NULL:

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

这是在类库程序集中吗?那些从不使用他们自己的 app.config - 而是使用主机应用程序的 app.config(使用类库的应用程序)。

马克

于 2009-10-21T18:29:51.393 回答
3

当您使用 FitNesse 进行测试时,实际运行的可执行文件是“FitServer.exe”,因此 AppSettings 会在 FitServer.exe 所在的目录中寻找“FitServer.exe.config”。因此,一个快速而肮脏的解决方案是将您的 app.config 复制到那里并重命名它。

更好的解决方案是指定应用程序配置,如下所述: http ://www.syterra.com/FitnesseDotNet/ApplicationConfigurationFile.html

或者如果您使用 fitSharp(它是 FitNesse.NET 的增强版): http ://www.syterra.com/Fit/AppConfigFiles.html

于 2009-10-21T19:28:50.793 回答
0

不要把它放在应用程序设置中。使用 <connectionStrings>

例子:

<app设置/>

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

</connectionStrings>

字符串 cnstr = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"].ToString();

于 2010-03-18T14:17:11.143 回答