ConfigurationSettings.AppSettings 为空,抛出 null 异常

发布于 2024-08-08 10:05:36 字数 1298 浏览 2 评论 0原文

我有一个这样的类:

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)并在代码顶部使用了语句。

我正在使用 Fitnesse 来测试代码,就在那时我收到了错误。据我了解,我还应该将 app.config 文件的副本放置在我已完成的测试装置项目的 Bin>Debug 文件夹中。所以,我不知道为什么我仍然收到此错误。

请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挖鼻大婶 2024-08-15 10:05:36

另外:尝试使用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 (使用类库的应用程序)。

马克

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

北音执念 2024-08-15 10:05:36

当您使用 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

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

一笔一画续写前缘 2024-08-15 10:05:36

不要将其放在应用程序设置中。使用

示例:

>

<连接字符串>
<添加名称=“NORTHWNDConnectionString”connectionString=“数据源=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;集成安全性=True;用户实例=True”providerName=“System.Data.SqlClient”/>

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

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();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文