Using appsettings.json to configure Kestrel listen port Dotnet core 2 preview 2

旧巷老猫 提交于 2019-11-30 00:11:06

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

See this issue comment:

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

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

As mentioned in a comment on the accepted answer, 2.1 has support for appsettings.json, see https://blogs.msdn.microsoft.com/webdev/2018/02/02/asp-net-core-2-1-roadmap/#security
A working appsettings.json:

"Kestrel": {
  "EndPoints": {
  "Http": {
  "Url": "http://localhost:5555"
 }}}

This is for a Program.cs using (created by "dotnet new webapi"):

WebHost.CreateDefaultBuilder(args)

Relevant source code in github https://github.com/aspnet/MetaPackages/blob/master/src/Microsoft.AspNetCore/WebHost.cs#L163

options.Configure(builderContext.Configuration.GetSection("Kestrel"));

and https://github.com/aspnet/MetaPackages/blob/master/src/Microsoft.AspNetCore/WebHost.cs#L169

config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

I am use Program.cs and hosting.json file to config Kestrel. Example:

var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("hosting.json", optional: true, reloadOnChange: true)
                .Build();

var host = new WebHostBuilder()
                .UseConfiguration(config)
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>();

hosting.json:

{
  "urls": "http://localhost:4444;http://localhost:4445;"
}

This is exapmle for the latest version dotnet core. For earlier versions: hosting.json:

{
  "server.urls": "http://localhost:4444;http://localhost:4445;"
}

I know that this is an old post but to run visual studio with kestrel.

just edit the appsettings.json and add the config like this (tested with NetCore 2.0 and 2.1)

    "profiles" : {
        "Kestrel": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "http://localhost:6969/"
        }
    }

I had the same issue whereby my Kestrel configuration in appsettings.json is not being picked up. From this article about migrating from asp.net core 2.0 to 2.1, I updated the bootstraping code to become like the below, and it worked for me.

    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args) 
            .UseStartup<Startup>();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!