简体   繁体   English

ASP.NET 核心 3.1:ConfigureAppConfiguration 是否与 launchSettings.json 交互?

[英]ASP.NET core 3.1: does ConfigureAppConfiguration interacts with launchSettings.json?

I'm experiencing a strange behavior when launching an ASP.NET core 3.1 web application from Visual Studio 2019 using kestrel (I mean the launching profile which does not use IIS express).我在使用 kestrel 从 Visual Studio 2019 启动 ASP.NET 核心 3.1 web 应用程序时遇到了一种奇怪的行为(我的意思是使用 Z5DA5ACF461B4EFB7E266EC861065B21 的启动配置文件)。

I have created a minimal application to reproduce the issue.我创建了一个最小的应用程序来重现该问题。

OS: Windows 10 (build 19041.746) Visual Studio version: Visual Studio 2019 version 16.8.4操作系统: Windows 10(内部版本 19041.746) Visual Studio 版本: Visual Studio 2019 版本 16.8.4

This is the csproj file:这是 csproj 文件:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>


</Project>

This is the launchSettings.json file:这是launchSettings.json文件:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:52222",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "TestWebApplication": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "applicationUrl": "http://localhost:5002",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

This is the Program.cs file:这是 Program.cs 文件:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace TestWebApplication
{
  public class Program
  {
    public static void Main(string[] args)
    {
      CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
              webBuilder.UseStartup<Startup>();
            });
  }
}

This is the Startup.cs file:这是 Startup.cs 文件:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace TestWebApplication
{
  public class Startup
  {
    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddControllers();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      if (env.IsDevelopment())
      {
        app.UseDeveloperExceptionPage();
      }

      app.UseRouting();

      app.UseAuthorization();

      app.UseEndpoints(endpoints =>
      {
        endpoints.MapControllers();
      });
    }
  }
}

When I launch this web application by using the TestWebApplication profile from Visual Studio everything works fine : kestrel is launched at http://localhost:5002 and the web browser is launched at http://localhost:5002/weatherforecast . When I launch this web application by using the TestWebApplication profile from Visual Studio everything works fine : kestrel is launched at http://localhost:5002 and the web browser is launched at http://localhost:5002/weatherforecast .

This is exactly what I would expect from the provided launchSettings.json file.这正是我对提供的launchSettings.json文件的期望。

Consider the following change to the Program.cs, made to customize the application configuration sources:考虑对 Program.cs 进行以下更改,以自定义应用程序配置源:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace TestWebApplication
{
  public class Program
  {
    public static void Main(string[] args)
    {
      CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration(builder =>
            {
              builder.Sources.Clear();
              builder.AddEnvironmentVariables();
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
              webBuilder.UseStartup<Startup>();
            });
  }
}

This change breaks everything in terms of interaction with the launchSettings.json file.此更改破坏了与launchSettings.json文件交互方面的所有内容。 The following happens:会发生以下情况:

  • kester is launched on both http://localhost:5000 and https://localhost:5001 kester 在http://localhost:5000https://localhost:5001上启动
  • web browser is not launched on http://localhost:5002/weatherforecast web 浏览器http://localhost:5002/weatherforecast上启动

Basically it seems that the file launchSettings.json is ignored and some defaults are used instead.基本上,似乎文件launchSettings.json被忽略了,而是使用了一些默认值。

I have also observed the following:我还观察到以下情况:

  • the root cause seems to be the line which clears the configuration sources: builder.Sources.Clear();根本原因似乎是清除配置源的行: builder.Sources.Clear();
  • the IIS express profile is not affected by this issue: it works as expected even with the modified Program.cs file IIS express 配置文件不受此问题的影响:即使使用修改后的 Program.cs 文件,它也可以按预期工作

Is this a bug?这是一个错误吗? Am I missing anything?我错过了什么吗?

SOME CLARIFICATIONS一些澄清

I'm clearing the configuration sources in order to have full control over the configuration sources themselves.我正在清除配置源以便完全控制配置源本身。 Put another way, I would like to remove all the existing configuration sources and start from scratch.换句话说,我想删除所有现有的配置源并从头开始。 According to this documentation this approach seems to be allowed.根据这个文档,这种方法似乎是允许的。

Notice that the exact same code works as expected when using the WebHost class instead of the Host class.请注意,使用WebHost class 而不是Host class 时,完全相同的代码可以正常工作。 In that case the configuration sources can be fully customized and the ability to read the launchSettings.json file is maintained for Kestrel too.在这种情况下,可以完全自定义配置源,并且 Kestrel 也可以读取launchSettings.json文件。 I'm trying to achieve the same effect with the generic Host class, which is the recommended class to be used instead of WebHost in ASP.NET core 3.1我正在尝试使用通用Host class 达到相同的效果,这是推荐的 class 用于代替 ASP.NET 核心 3.1 中的WebHost

I have finally managed this issue.我终于解决了这个问题。

That's not an ASP.NET core 3.1 bug, insted this is the way the generic Host is designed to work.这不是 ASP.NET 核心 3.1 错误,这是通用Host设计的工作方式。

Basically the configuration for the port number which is specified inside the lanchSettings.json file is passed to the ASP.NET core application by means of some environment variables prefixed by ASPNETCORE_ .基本上,在lanchSettings.json文件中指定的端口号配置通过一些以ASPNETCORE_为前缀的环境变量传递给 ASP.NET 核心应用程序。 These environment variables are set automatically by Visual Studio.这些环境变量由 Visual Studio 自动设置。 The one responsible for the kestrel port binding is called ASPNETCORE_URLS .负责红隼端口绑定的称为ASPNETCORE_URLS

When doing builder.Sources.Clear();当做builder.Sources.Clear(); the configuration source for the environment variables prefixed by ASPNETCORE_ is cleared away.ASPNETCORE_为前缀的环境变量的配置源被清除。 So, in order to fix the issue, it is necessary to replace this configuration source:所以,为了解决这个问题,有必要替换这个配置源:

.ConfigureAppConfiguration(builder =>
            {
              builder.Sources.Clear();
              builder.AddEnvironmentVariables();
              builder.AddEnvironmentVariables("ASPNETCORE_");
            })

All the details are available here所有详细信息都可以在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Asp.NET Core 3.1 Startup 类访问 launchSettings.json 中的 `applicationUrl` 属性? - How Do You Access the `applicationUrl` Property Found in launchSettings.json from Asp.NET Core 3.1 Startup class? 在调试中为SSL配置launchSettings.json - ASP.NET Core / Visual Studio Code - Configure launchSettings.json for SSL in debug - ASP.NET Core / Visual Studio Code ASP.NET Core 2.2 如何在项目之间共享 web.config/launchSettings.json 文件 - ASP.NET Core 2.2 How to share web.config/launchSettings.json files among projects 您可以在 ASP.NET Core 项目中更改 launchSettings.json 的位置吗? - Can you change the location of launchSettings.json in an ASP.NET Core project? 从Mac上的命令行运行ASP.NET 5应用程序时是使用launchSettings.json吗? - Is launchSettings.json used when running ASP.NET 5 apps from the command line on Mac? 在 ASP.NET Core 的 ConfigureAppConfiguration 方法中使用日志记录 - Use logging in ConfigureAppConfiguration method in ASP.NET Core asp.net core 3.1 上的 Json 请求 - Json request on asp.net core 3.1 launchsettings.json 是否在生产中使用的 dotnet 核心项目中? - Is launchsettings.json in a dotnet core project used in production? Json Object Asp.net 核心 3.1 的最大长度 - Max Length for Json Object Asp.net Core 3.1 用于 MSTest [ASP.NET Core 3.1] 的 appsettings.json - appsettings.json for MSTest [ASP.NET Core 3.1]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM