ASP.NET 核心 3.1:ConfigureAppConfiguration 是否与 launchSettings.json 交互? [英] ASP.NET core 3.1: does ConfigureAppConfiguration interacts with launchSettings.json?

查看:23
本文介绍了ASP.NET 核心 3.1:ConfigureAppConfiguration 是否与 launchSettings.json 交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 kestrel 从 Visual Studio 2019 启动 ASP.NET Core 3.1 Web 应用程序时遇到一个奇怪的行为(我的意思是启动配置文件使用 IIS express).

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).

我创建了一个最小的应用程序来重现该问题.

I have created a minimal application to reproduce the issue.

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

OS: Windows 10 (build 19041.746) Visual Studio version: Visual Studio 2019 version 16.8.4

这是 csproj 文件:

This is the csproj file:

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

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


</Project>

这是launchSettings.json 文件:

This is the launchSettings.json file:

{
  "$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"
      }
    }
  }
}

这是 Program.cs 文件:

This is the Program.cs file:

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>();
            });
  }
}

这是 Startup.cs 文件:

This is the Startup.cs file:

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();
      });
    }
  }
}

当我使用 Visual Studio 中的 TestWebApplication 配置文件启动此 Web 应用程序时一切正常:kestrel 在 http://localhost:5002<启动/strong> 并在 http://localhost:5002/weatherforecast 处启动 Web 浏览器.

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.

这正是我对提供的 launchSettings.json 文件的期望.

This is exactly what I would expect from the provided launchSettings.json file.

考虑对 Program.cs 进行以下更改,以自定义应用程序配置源:

Consider the following change to the Program.cs, made to customize the application configuration sources:

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>();
            });
  }
}

此更改破坏了与 launchSettings.json 文件交互方面的所有内容.发生以下情况:

This change breaks everything in terms of interaction with the launchSettings.json file. The following happens:

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

基本上似乎文件 launchSettings.json 被忽略了,而是使用了一些默认值.

Basically it seems that the file launchSettings.json is ignored and some defaults are used instead.

我还观察到以下几点:

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

这是一个错误吗?我错过了什么吗?

Is this a bug ? Am I missing anything ?

一些说明

我正在清除配置源,以便完全控制配置源本身.换句话说,我想删除所有现有的配置源并从头开始.根据 this文档 这种方法似乎是允许的.

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.

请注意,使用 WebHost 类而不是 Host 类时,完全相同的代码按预期工作.在这种情况下,可以完全自定义配置源,并且 Kestrel 也可以读取 launchSettings.json 文件.我正在尝试使用通用 Host 类实现相同的效果,这是在 ASP.NET 核心 3.1 中推荐使用的类,而不是 WebHost

Notice that the exact same code works as expected when using the WebHost class instead of the 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. 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

推荐答案

我终于解决了这个问题.

I have finally managed this issue.

这不是 ASP.NET Core 3.1 的错误,这是通用 Host 设计的工作方式.

That's not an ASP.NET core 3.1 bug, insted this is the way the generic Host is designed to work.

基本上,lanchSettings.json 文件中指定的端口号配置通过一些以ASPNETCORE_.这些环境变量由 Visual Studio 自动设置.负责 kestrel 端口绑定的那个称为 ASPNETCORE_URLS.

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_. These environment variables are set automatically by Visual Studio. The one responsible for the kestrel port binding is called ASPNETCORE_URLS.

在执行 builder.Sources.Clear(); 时,以 ASPNETCORE_ 为前缀的环境变量的配置源被清除.所以,为了解决这个问题,有必要更换这个配置源:

When doing builder.Sources.Clear(); the configuration source for the environment variables prefixed by ASPNETCORE_ is cleared away. 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_");
            })

所有详细信息都可以在这里

这篇关于ASP.NET 核心 3.1:ConfigureAppConfiguration 是否与 launchSettings.json 交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆