【问题标题】:ConfigurationManager.AppSettings is always emptyConfigurationManager.AppSettings 始终为空
【发布时间】:2020-05-03 06:24:58
【问题描述】:

我搜索的主题

我的应用程序是一个 .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,就好像没有被解析一样。

有什么想法吗?

【问题讨论】:

  • 确定你不是想拥有一个 appSettings.json 文件?
  • 我有一个,是的,但是docs.microsoft.com/de-de/dotnet/api/… 说我需要一个 .config 文件。我需要一个 appSettings.json 文件吗?
  • 点网核心不使用.config文件和配置管理器
  • @Nkosi 是的,我读到了,但是在将它添加为 NuGet 包时支持它(至少我是通过 Google 发现的)。还有其他获取内容的方法吗?
  • @TanveerBadar 你是对的。我说错了。它不再默认使用它。您现在必须显式添加/引用它才能使用它。

标签: c# .net .net-core


【解决方案1】:

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

目前我无法使用appsettings.json,所以我不得不将我的设置从web.config 移动到app.config。这好像是 .net core ConfigurationManager works only with app.config 的版本。

【讨论】:

    【解决方案2】:

    我想为没有选择使用 appsettings.json 的任何人添加...

    对我来说,这个问题体现在 UnitTest 项目中。 ConfigurationManager 需要一个名为 ASSEMBLYNAME.dll.config 的文件,但 .net 核心中的单元测试以名称“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

    【讨论】:

    • 如果在 VS 中使用 Rider 或 Resharper 的测试运行程序,您还需要以下内容:&lt;Copy SourceFiles="@(FilesToCopy)" DestinationFiles="$(OutputPath)ReSharperTestRunner.dll.config" /&gt; 如果这不起作用,请进入 ConfigurationManager.AppSettings 并查看 @987654325 @ 并找到 ExePath 变量。
    【解决方案3】:

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

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

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

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

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

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-09
      • 2017-08-15
      • 2017-07-31
      • 2019-07-18
      • 2019-08-18
      • 2011-12-29
      • 2019-07-02
      • 2019-05-04
      相关资源
      最近更新 更多