appsettings.json(开发和生产)被 ASP.NET Core 2.0 忽略?

appsettings.json (both dev and prod) ignored by ASP.NET Core 2.0?

我想为我的 ASP.NET 核心应用程序设置 HTTPS 以及定义我的应用程序可以侦听的不同端口(例如,有点像 Scot Hanselman 刚才展示的内容:https://channel9.msdn.com/Events/Build/2017/B8048).

我查看了官方资源:

我认为默认构建器应该挖掘 appsettings.json(或相关的 appsettings.Development.json)。

Program.cs内容:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        var builder = WebHost.CreateDefaultBuilder(args);
        return builder.UseStartup<Startup>().Build();
    }
}

appsettings.Development.json内容:

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"
      }
    }
  },
  "Kestrel": {
    "Endpoints": {
      "Localhost": {
        "Address": "127.0.0.1",
        "Port": "5001"
      }
    }
  },
  "LocalhostwithHttps": {
    "Address": "127.0.0.1",
    "Port": "44333",
    "Certificate": {
      "Source": "Store",
      "StoreLocation": "LocalMachine",
      "StoreName": "My",
      "Subject": "CN=localhost"
    }
  }
}

唉,当开始针对开发环境进行调试时,我的 Web 应用程序以这样的方式结束:

Hosting environment: Development
Content root path: c:\Users\ehoua\Desktop\There
Launching browser (cmd.exe /C start http://localhost:5000)
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

我是否真的必须放弃默认构建器并指定应用程序需要依赖的应用程序设置或硬编码端口值...?

[编辑] 在 @Set 回答之后,我在 Program.cs 中尝试了以下操作:

public static IWebHost BuildWebHost(string[] args)
{
    var developmentConfiguration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile($"appsettings.Development.json", optional: false)
    .Build();

    var productionConfiguration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile($"appsettings.json", optional: false)
    .Build();

    return WebHost.CreateDefaultBuilder(args)
    .UseEnvironment("Development").UseConfiguration(developmentConfiguration)
    .UseEnvironment("Production").UseConfiguration(productionConfiguration)
    .UseStartup<Startup>().Build();
}

我特意将可选参数设置为 false,以便仔细检查 appsettings 文件是否已正确添加到配置中。还检查了 Startup Configure 方法中的环境是 Development 还是 Production 但仍然以属于我的 appsettings 文件的 none 的相同旧端口 5000 结束。

[编辑] 在 .NET 2.1 中可能

参见:https://github.com/aspnet/KestrelHttpServer/issues/1290#issuecomment-353416087

看来您需要直接调用 UseConfiguration 来设置从 appsettings.json:

读取的 URL 地址
public static IWebHost BuildWebHost(string[] args)
{
        var config = new ConfigurationBuilder()
            .AddJsonFile($"appsettings.Development.json", optional: true)
            .Build();

        return WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(config)
            .UseStartup<Startup>()
            .Build();
}

CreateDefaultBuilder 方法实现在内部使用 ConfigureAppConfiguration 方法,而不是 UseConfiguration。并且有区别(来自相关Github issue):

The ConfigureAppConfiguration was intended to configure the IConfiguration in the application services whereas UseConfiguration is intended to configure the settings of the WebHostBuilder. Since the url address is a setting on the WebHostBuilder only UseConfiguration will work here.

Note that the WebHostBuilder's configuration is added to the application's configuration but the converse is not true; configuring the application's configuration does not affect the WebHostBuilder's configuration.


代替 "Kestel.Endpoints" 部分,您的 appsetting.json 应该只包含以下内容:

{
   ...
   "urls": "http://127.0.0.1:5001"
}