使用 appsettings.json 配置 Kestrel 监听端口 Dotnet core 2 preview 2 [英] Using appsettings.json to configure Kestrel listen port Dotnet core 2 preview 2

查看:65
本文介绍了使用 appsettings.json 配置 Kestrel 监听端口 Dotnet core 2 preview 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,为 ASP Dotnet Core 2 preview 1/2 设置监听端口的正确方法是在 appsettings.json 中创建一个 Kestrel 部分,格式如下:

From what I understand the correct way of setting listen ports for ASP Dotnet Core 2 preview 1/2 is by creating a Kestrel section in the appsettings.json in the following format:

"Kestrel": {
    "EndPoints": { //Could also be Endpoints, it's a bit unclear
        "Http": {
        "Address": "127.0.0.1",
    "Port": 9001 //the port you want Kestrel to run on
},

我尝试在 Debian 机器上设置示例 web 应用程序,但是当我启动应用程序时,它写出应用程序在端口 5000 上列出,默认端口..

I have tried to set up the sample webapp on a Debian machine, but when I start the app, it writes out that the app is listing on port 5000, the default port..

我知道 appsettings.json 已被读取,因为当我将日志记录级别更改为 Trace 时,我会在启动时获得更多信息,包括未找到端点并且应用程序将使用标准 5000 端口.

I know that the appsettings.json is read, because when I change the logging level to Trace, I get more info upon startup, including that no Endpoints are found and the app will use the standard 5000 port.

我尝试在 Github 上搜索 aspnet 源代码,我可以找到从配置中读取 Kestrel 部分的区域(https://github.com/aspnet/Identity/blob/e38759b8a2de1b7a4a1c19462e40214b43c1cf3b43c1cf3b/samples/idcf3b43c1cf3b/samplesServera/idcf3b/samples/idcf3b/samples/idcf3b/samples/idcf3b.你可以看到它看起来像一个示例项目.

I have tried to search the aspnet source code on Github, and I can find a area where the Kestrel section is read from configuration (https://github.com/aspnet/Identity/blob/e38759b8a2de1b7a4a1c19462e40214b43c1cf3b/samples/IdentityOIDCWebApplicationSample/MetaPackage/KestrelServerOptionsSetup.cs), but as you can see it looks like a sample project.

我错过了什么,这不是在 ASP Dotnet core 2 中配置 Kestrel 的标准方法吗?

What am I missing, isn't this the standard way to configure Kestrel in ASP Dotnet core 2?

推荐答案

2.0 中不再支持通过 appsettings.json 进行 Kestrel 配置.

Support for Kestrel configuration via appsettings.json has been dropped in 2.0.

请参阅问题评论:

kestrel 配置文件支持从 2.0.0 中删除.需要在初始化代码中手动读取配置值.

kestrel config file support was cut from 2.0.0. Config values need to be read manually in your initialization code.

要解决这个问题,您可以在 program.cs 中执行以下操作:

To get around this, you can do something like this in program.cs:

public static IWebHost BuildWebHost(string[] args) =>
 WebHost.CreateDefaultBuilder(args)
 .UseStartup < Startup > ()
 .UseKestrel((hostingContext, options) => 
 { 
  if (hostingContext.HostingEnvironment.IsDevelopment) {
   options.Listen(IPAddress.Loopback, 9001);
   options.Listen(IPAddress.Loopback, 9002, listenOptions => {
    listenOptions.UseHttps("certificate.pfx", "password");
   });
  }
 })
 .Build();

这篇关于使用 appsettings.json 配置 Kestrel 监听端口 Dotnet core 2 preview 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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