首页 > 解决方案 > Azure AppService 上加载了错误的设置

问题描述

我有一个具有身份服务器设置的 asp.net 核心应用程序。为此,我有一个 appsettings.json、一个 appsettings.Development.json 和一个 appsettings.Production.json。

appsettings.json:

..
  "ApplicationInsights": {
    "InstrumentationKey": ""
  },
  "IdentityServer": {
    "Clients": {
      "ApplySupportTool.Client": {
        "Profile": "IdentityServerSPA"
      }
    }
  },
  "BuildInfo": {
    "Environment": "Integration",
    "Version": "1.0.0 Beta"
  },
..

appsettings.Development.json

{
  "IdentityServer": {
    "Key": {
      "Type": "Development"
    }
  }
}

appsettings.Production.json

{
  "IdentityServer": {
    "Key": {
      "Type": "Store",
      "StoreName": "My",
      "StoreLocation": "CurrentUser",
      "Name": "[Name]"
    }
  }
}

在本地工作没有问题,我可以通过从启动设置中删除环境变量在它们之间切换:

    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    }

在加载类似于默认模板的设置文件方面,我的 HostBuilder 是这样创建的,没有对设置文件进行特殊添加。

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        return Host.CreateDefaultBuilder(args)
                   .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                   .ConfigureWebHostDefaults(webHostBuilder =>
                   {
                       webHostBuilder
                          .UseContentRoot(Directory.GetCurrentDirectory())
                          .UseStartup<Startup>()
                          .UseAzureAppServices();
                   })
                   .UseSerilog();
    }

当我签入 Startup.cs 的构造函数时,我可以验证它是否也加载了附加文件。

但是当我将它部署到 Azure 时,似乎总是会加载开发版本。据我所知,没有设置任何值,它应该用于生产(在本地工作)。可以肯定的是,我还尝试将值为“Production”的 ASPNETCORE_ENVIRONMENT 显式添加到我的 AppService 中。但这也没有改变任何东西。我必须以特殊方式加载它吗?

标签: azureasp.net-core

解决方案


希望 Development 不会在 Program.cs 中保持最后,它会覆盖您的 appsetting 和 production.json

var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();

同样在 WebAPP 刀片中,如果您要覆盖 ASPNETCORE_ENVIRONMENT 变量,则需要重新启动实例以反映并重新加载更改。

此外,有关您的 program.cs 的更多信息将有助于我们找到根本原因。


推荐阅读