簡體   English   中英

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

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

我在使用 kestrel 從 Visual Studio 2019 啟動 ASP.NET 核心 3.1 web 應用程序時遇到了一種奇怪的行為(我的意思是使用 Z5DA5ACF461B4EFB7E266EC861065B21 的啟動配置文件)。

我創建了一個最小的應用程序來重現該問題。

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

這是 csproj 文件:

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

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


</Project>

這是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"
      }
    }
  }
}

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

這是 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 .

這正是我對提供的launchSettings.json文件的期望。

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

此更改破壞了與launchSettings.json文件交互方面的所有內容。 會發生以下情況:

  • kester 在http://localhost:5000https://localhost:5001上啟動
  • web 瀏覽器http://localhost:5002/weatherforecast上啟動

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

我還觀察到以下情況:

  • 根本原因似乎是清除配置源的行: builder.Sources.Clear();
  • IIS express 配置文件不受此問題的影響:即使使用修改后的 Program.cs 文件,它也可以按預期工作

這是一個錯誤嗎? 我錯過了什么嗎?

一些澄清

我正在清除配置源以便完全控制配置源本身。 換句話說,我想刪除所有現有的配置源並從頭開始。 根據這個文檔,這種方法似乎是允許的。

請注意,使用WebHost class 而不是Host class 時,完全相同的代碼可以正常工作。 在這種情況下,可以完全自定義配置源,並且 Kestrel 也可以讀取launchSettings.json文件。 我正在嘗試使用通用Host class 達到相同的效果,這是推薦的 class 用於代替 ASP.NET 核心 3.1 中的WebHost

我終於解決了這個問題。

這不是 ASP.NET 核心 3.1 錯誤,這是通用Host設計的工作方式。

基本上,在lanchSettings.json文件中指定的端口號配置通過一些以ASPNETCORE_為前綴的環境變量傳遞給 ASP.NET 核心應用程序。 這些環境變量由 Visual Studio 自動設置。 負責紅隼端口綁定的稱為ASPNETCORE_URLS

當做builder.Sources.Clear(); ASPNETCORE_為前綴的環境變量的配置源被清除。 所以,為了解決這個問題,有必要替換這個配置源:

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

所有詳細信息都可以在這里找到

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM