【问题标题】:app.config migration for .net framework 4.x assemblies referenced in Core 2.0 Web API project?核心 2.0 Web API 项目中引用的 .net 框架 4.x 程序集的 app.config 迁移?
【发布时间】:2018-02-23 16:43:32
【问题描述】:

我有一个 .net 核心 web api 项目,它引用了具有 app.config 设置的 4.x 程序集。我是否可以在不修改旧程序集的情况下将 app.config 设置转换为 appsettings.json,或者我只是将旧程序集的设置放入 app.config 文件中?如果我只是放入 app.config 文件,如何在发布时更改这些设置?

【问题讨论】:

    标签: c# asp.net .net asp.net-core-webapi asp.net-core-mvc-2.0


    【解决方案1】:

    Core 2.0 Web API 中,您也可以通过这种方式将您的配置指向旧的app.config

    IConfigurationRoot configuration = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: true)
        .AddXmlFile("app.config")
        .Build();
    

    然后,您将能够通过键(看起来像冒号分隔的 XML 路径)访问例如您的连接字符串(和其他应用程序设置)。例如,对于连接字符串部分,您可以通过这种方式获取连接名称 MyConnectionName 的值:

    key: "connectionStrings:add:MyConnectionName:connectionString" 
    value: "...your connection string..."
    

    或者您可以按照here 的详细描述实现您自己的LegacyConfigurationProvider,并拥有如下内容:

    IConfigurationRoot configuration = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: true)
        .Add(new LegacyConfigurationProvider())
        .Build();
    

    另见Configuration in ASP.NET Core

    【讨论】:

    • 我们在 VS 中使用 Core 2.0 项目模板。该模板中的 Startup.cs 不使用“IConfigurationRoot”或 ConfigurationBuilder。这些在 Core 1.x 中使用。您有使用 Core 2.0 的默认项目模板 Startup.cs 的示例吗?
    • 只需添加 public IConfigurationRoot Configuration { get;放; } 属性给你启动文件,然后是 services.Configure(Configuration);详情见docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration
    • 所以让它像 Core 1.x 一样?我不能只将“public IConfigurationRoot Configuration { get; set; }”添加到现有模板中,因为它已经声明了“public IConfiguration Configuration { get; }”。从头开始创建一个 2.0 项目,这样你就可以明白我的意思了。
    【解决方案2】:

    所以我最终做的只是添加 app.config 文件。我还添加了一个 app.release.config 并将其重命名为 app.config 作为我发布过程的一部分。我无法使用配置转换。我根本无法让它在 Azure CI 中工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-01
      • 2017-11-29
      • 1970-01-01
      • 2020-12-18
      • 2020-06-21
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多