【问题标题】:.NET 5 C# App.config Section names beginning with config are reserved.NET 5 C# App.config 保留以 config 开头的部分名称
【发布时间】:2022-11-17 20:08:54
【问题描述】:

我在.NET 5.0App.config 有申请,想添加自定义配置。在以前版本的 .NET 中有效,但在 .NET 5.0 中无效。这是App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
  </configSections>

  <configBuilders>
    <builders>
    </builders>
  </configBuilders>

  <appSettings>
    <add key="testProperty" value="aaaa" />
  </appSettings>

</configuration>

当我运行这个应用程序并尝试读取 testProperty 时:

 var testProperty = System.Configuration.ConfigurationManager.AppSettings["testProperty"];

我收到错误:

Section names beginning with config are reserved

相同的代码在.NET 4.7.1 中有效,但在.NET 5.0 中无效

【问题讨论】:

  • 这部分的类型很可能是错误的。我的意思是 System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a < 这个。在 .NET 5 中可能有所不同

标签: c# .net-5 app-config


【解决方案1】:

大概这就是答案: .NET 6.0 no longer use App.config

随着其他库升级到 .NET,它们将通过支持实现现代化 appsettings.json 而不是 App.config。例如,日志提供者 在已针对 .NET 6 升级的 .NET Framework 中不再使用 App.config 进行设置。听从他们的指示对你有好处 并且不再使用 App.config。

【讨论】:

    猜你喜欢
    • 2017-03-18
    • 2018-08-24
    • 2015-10-06
    • 2012-06-26
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    相关资源
    最近更新 更多