ConfigurationManager.AppSettings 总是为空

5

我搜索到的主题包括:

我的应用程序是一个.NET Core 3.1应用程序,因此我通过NuGet向我的项目添加了System.Configuration.ConfigurationManager库。我的根文件夹包含以下内容的Web.Config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <compilation debug="true"  />
        <httpRuntime  />
    </system.web>
    <appSettings>
        <!-- Folder of LogParser job-configurations -->
        <add key="JobFolder" value="App_Data/jobs"/>
        <!-- Background task execution interval in seconds -->
        <add key="Interval" value="5"/>
        <!-- Message offset in seconds. Reads older messages to circumvent log4net timestamp bug -->
        <add key="Offset" value="7200"/>
        <!-- Caching duration for hash MemoryCache in seconds. Default is 604800 (7 days) -->
        <add key="Caching" value="604800"/>
    </appSettings>
</configuration>

然而,当我访问ConfigurationManager.AppSettings[key]时,它总是为空。此外,Configurationmanager.AppSettings.AllKeys也为空,计数为0,就像它没有被解析一样。
有什么想法吗?

3
你确定不是想要一个 appSettings.json 文件吗? - Tony Abrams
我曾经有一个问题,但是https://learn.microsoft.com/de-de/dotnet/api/system.configuration.configurationmanager?view=netframework-4.8说我需要一个.config文件。那我需要一个appSettings.json文件吗? - Musterknabe
Dot net core 不使用 .config 文件和配置管理器。 - Nkosi
@Nkosi 是的,我看到了,但是只有在将其作为NuGet包添加时才支持(至少我通过Google发现了)。还有其他获取内容的方法吗? - Musterknabe
@Nkosi 可能不被支持,但它运行良好,必须是其他原因。我知道这一点,因为我个人在许多应用程序中使用过它。 - Tanveer Badar
1
@TanveerBadar,你说得对。我说错了。现在它不再默认使用它了。你必须显式地添加/引用它才能使用它。 - Nkosi
3个回答

12

我想对于任何没有使用appsettings.json选项的人进行补充...

对我而言,这个问题在一个单元测试项目中表现出来。 ConfigurationManager期望一个名为ASSEMBLYNAME.dll.config的文件, 但是.NET Core中的单元测试运行在名称为"testhost"的环境下,因此它会寻找testhost.dll.config。 因此,您需要将生成的配置文件重命名为ConfigurationManager正在寻找的文件名。

在您的csproj文件中添加一个构建步骤,如下所示...

<Target Name="CopyAppConfig" AfterTargets="Build" DependsOnTargets="Build">
    <CreateItem Include="$(OutputPath)$(AssemblyName).dll.config">
      <Output TaskParameter="Include" ItemName="FilesToCopy"/>
    </CreateItem>
    <Copy SourceFiles="@(FilesToCopy)" DestinationFiles="$(OutputPath)testhost.dll.config" />
</Target>

解决方案来自于https://github.com/microsoft/testfx/issues/348


1
如果在VS中使用Rider或者Resharper的测试运行器,你还需要以下内容:<Copy SourceFiles="@(FilesToCopy)" DestinationFiles="$(OutputPath)ReSharperTestRunner.dll.config" />如果这个方法不起作用,可以进入ConfigurationManager.AppSettings并查看s_configSystem._completeConfigRecord.ConfigContext,找到ExePath变量。 - nullPainter
在你的调试器中,你可以使用 System.Reflection.Assembly.GetEntryAssembly().Location 来确定预期的名称是什么。对我来说,它看起来是这样的(缩写):"C:\Program Files (x86)\JetBrains\..\ReSharperTestRunner.dll" 这意味着我的 .config 文件需要被命名为 ReSharperTestRunner.dll.config - undefined

0

当我将asp应用从.net 4.x迁移到.net core 3.1时,我遇到了这个问题。为了保留依赖于ConfigurationManager的功能,我还安装了nugetSystem.Configuration.ConfigurationManager,但是ConnectionStringsAppSettings都为空。

目前我无法使用appsettings.json,所以我不得不将我的设置从web.config移动到app.config。看起来.net core版本的ConfigurationManager仅适用于app.config


这个可以工作,但不是我想要的答案。 - llessurt

0

好的,https://stackoverflow.com/users/392957/tony-abrams 给了我正确的方向。

所以基本上,我需要一个 appsettings.json 文件(即使互联网告诉我不需要),我像这样定义它:

{
    "JobFolder": "App_Data/jobs",
    "Interval": 5,
    "Offset": 7200,
    "Caching": 604800
}

然后,在我需要的类中,我添加了一个新的构造函数参数IConfiguration configuration,它已经在DI容器中注册,因此不需要进一步操作。

然后,当我想要访问该值时,我可以简单地执行_configuration.GetValue<string>("JobFolder")并获得该值。

谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接