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

6

37

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
},

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

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.

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.

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

Epley answered 20/7, 2017 at 9:52 Comment(0)
K
12

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();
Keller answered 12/10, 2017 at 1:42 Comment(2)
.UseKestrel((hostingContext, options) => { var env = hostingContext.HostingEnvironment; } Cleaner than: var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");Idun
This will become redundant when 2.1 is released as suuport for Kestrel via configuration will be back, and HTTPS in development will be on by default blogs.msdn.microsoft.com/webdev/2018/02/02/….Keller
G
66

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)
Gilmour answered 29/8, 2018 at 12:41 Comment(0)
K
12

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();
Keller answered 12/10, 2017 at 1:42 Comment(2)
.UseKestrel((hostingContext, options) => { var env = hostingContext.HostingEnvironment; } Cleaner than: var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");Idun
This will become redundant when 2.1 is released as suuport for Kestrel via configuration will be back, and HTTPS in development will be on by default blogs.msdn.microsoft.com/webdev/2018/02/02/….Keller
H
4

I am using Program.cs and hosting.json file to configure 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 above is an example for the latest version dotnet core.

For earlier versions:

hosting.json:

{
    "server.urls": "http://localhost:4444;http://localhost:4445;"
}
Habilitate answered 28/7, 2017 at 18:11 Comment(4)
This his the similar way I have configured Kestrel in core 1.1, and it's i possible to do it this way, but I have read multiple places that the future way of doing it should be by using the Kestrel section in appsettings.json. codingblast.com/asp-net-core-2-previewEpley
I saw that video and it looked so simple. I've been trying myself using the same method and I cannot get it to work either. Anyone else have any luck with this?Sulphanilamide
I'm also having this problem with asp.net core 2.0. Nothing I put in appsettings.json gets used. I don't understand it because appsettings should be read by default in the new asp.net core with the default configuration builder.Keller
@tura08 , its possible to use appsettings.json, see here: #46696615Modifier
C
2

To run Visual Studio with Kestrel, just edit 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/"
    }
}
Carrier answered 3/4, 2018 at 19:37 Comment(0)
R
0

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>();
}
Reversible answered 14/8, 2018 at 7:48 Comment(0)
C
0

Add the "Kestrel" section to the appsettings.json and run your app via the command line. BTW I strongly recommend avoiding the use of SSL certificates via hardcode. It worked for me on dotnet 6-8:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Hangfire": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://0.0.0.0:80"
      },
      "Https": {
        "Url": "https://0.0.0.0:443"
      }
    },
    "Certificates": {
      "Default": {
        "Path": "********.pfx",
        "Password": "1234"
      }
    }
  },
  "AllowedHosts": "*",
.... blah blah json
Coadjutrix answered 19/1 at 10:6 Comment(2)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Asturias
seriously. explain your answer!Shadrach

© 2022 - 2024 — McMap. All rights reserved.