ASP.NET Core 2无法从appsettings读取终结点 [英] ASP.NET Core 2 not reading endpoints from appsettings

查看:117
本文介绍了ASP.NET Core 2无法从appsettings读取终结点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ASP.NET Core 2.0通过appsettings.json中的设置配置Kestrel端点的新方法.这在Microsoft Build 2017活动中得到了证明(请参见 YouTube 时间范围21:00-26:20).

I'm trying to use ASP.NET Core 2.0's new way of configuring Kestrel endpoints via settings in appsettings.json. This was demonstrated at the Microsoft Build 2017 event (see it on YouTube timeframe 21:00-26:20).

发言者说(并演示了)以下内容将配置Kestrel上的侦听端口:

The speaker said (and demonstrated) that the following would configuring the listening ports on Kestrel:

{
   "Message": "TEST!!",
   "Kestrel": {
      "Endpoints": {
         "Localhost": {
            "Address": "127.0.0.1",
            "Port": "5011"
         }
      }
   }
}

但这对我没有用.当我使用"dotnet run"时,默认端口5000仍在使用.我知道正在加载appsettings.json文件,因为我可以在其他地方使用Message值.

But this hasn't worked for me. When I use 'dotnet run', the default port of 5000 is still being used. I know the appsettings.json file is being loaded, because I can use the Message value elsewhere.

GitHub 上的代码中,我看不到正在配置Kestrel的任何地方.

From the code on GitHub, I can't see any place where Kestrel is being configured.

还有其他人能够使这种方法起作用吗?我无法理解它在演示中的工作原理,但无法理解我自己的代码.

Has anyone else been able to get this method to work? I can't understand how it works in the demo but not in my own code.

推荐答案

我知道这是在不久前问到的,但是我认为在ASP.NET Core框架已得到增强以支持该问题时,将其张贴在这个问题上是很有意义的.自最初提出此问题以来,就从应用程序设置中进行了Kestrel端点配置. 请在下面找到更多详细信息,以及有关如何在ASP.NET Core 2.1和更早版本中实现该功能的简要概述.

I know this was asked a while ago, but I think it would make sense to post on this one as the ASP.NET Core framework has been enhanced to support Kestrel endpoint configuration from application settings since this question was initially asked. Please find below more details and a little summary on how to achieve that in ASP.NET Core 2.1 and earlier versions.

从ASP.NET Core 2.1版开始,可以在应用程序设置中配置Kestrel端点,如

It is possible to configure Kestrel endpoints in application settings starting from ASP.NET Core version 2.1, as also stated in the documentation.

有关更多详细信息,可以检入实际的 WebHost.CreateDefaultBuilder()实现: 从版本2.1开始,它将加载Kestrel配置部分,而在ASP.NET Core早期版本中,在设置WebHost时必须显式调用UseKestrel().

For further details, it is possible to check in the actual WebHost.CreateDefaultBuilder() implementation: from release 2.1, it loads the Kestrel configuration section, while on ASP.NET Core earlier versions it is necessary to explicitly call UseKestrel() when setting up your WebHost.

ASP.NET Core 2.1设置

在ASP.NET Core 2.1上,您可以通过简单地提供以下appsettings.json来将Kestrel设置为在http://127.0.0.1:5011处监听:

On ASP.NET Core 2.1, you can setup Kestrel to listen at http://127.0.0.1:5011 by simply providing the following appsettings.json:

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://127.0.0.1:5011" 
      }
    }
  }
}

请注意,您将需要使用Url而不是AddressPort.

Note you will need to use Url instead of Address and Port.

ASP.NET Core 2.0设置

在ASP.NET Core 2.0上,可以通过在Program.cs中显式调用UseKestrel()来实现相同的行为.您可以使用扩展方法来保持您的Program.cs干净:

On ASP.NET Core 2.0 this very same behavior can be achieved by explicitly calling UseKestrel() in Program.cs. You may use an extension method to keep your Program.cs clean:

public class Program {

    public static void Main(string[] args) {

        // configuration
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true)
            .AddCommandLine(args)
            .Build();

        // service creation
        var webHost = WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrelEndpoints(config) // extension method to keep things clean
            .UseConfiguration(config)
            .UseApplicationInsights()
            .Build();

        webHost.Run();

    }

}

UseKestrelEndpoints()是一种扩展方法,可查看实际的配置设置并相应地设置Kestrel选项:

The UseKestrelEndpoints() is an extension method that looks into the actual configuration settings and setup Kestrel options accordingly:

public static IWebHostBuilder UseKestrelEndpoints(this IWebHostBuilder webHostBuilder, IConfiguration configuration) {

        var endpointSection = configuration.GetSection("kestrel:endpoints");

        if (endpointSection.Exists()) {

            webHostBuilder.UseKestrel(options => {

                // retrieve url
                var httpUrl = endpointSection.GetValue<string>("http:url", null);

                /* setup options accordingly */

            });

        }

        return webHostBuilder;

    }

如果需要,您可以以类似方式设置HTTPS .

这篇关于ASP.NET Core 2无法从appsettings读取终结点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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