ASP.NET Core 2 not reading endpoints from appsettings

4.5k views Asked by At

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

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

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.

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.

2

There are 2 answers

3
GlennSills On

Unfortunately that doesn't work anymore. Check out How to use Kestrel in ASP.NET Core apps . So it doesn't really work with settings anymore. You can do something like the this in you program.cs. Here's I'm setting things up to use https locally but you get the general idea...

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

    public static IWebHost BuildWebHost(string[] args)
    {
        IHostingEnvironment env = null;

        return WebHost.CreateDefaultBuilder(args)
              .UseStartup<Startup>()
              .ConfigureAppConfiguration((hostingContext, config) =>
              {
                  env = hostingContext.HostingEnvironment;

                  config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

                  if (env.IsDevelopment())
                  {
                      var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
                      if (appAssembly != null)
                      {
                          config.AddUserSecrets(appAssembly, optional: true);
                      }
                  }

                  config.AddEnvironmentVariables();

                  if (args != null)
                  {
                      config.AddCommandLine(args);
                  }
              })
              .UseKestrel(options =>
              {
                  if (env.IsDevelopment())
                  {
                      options.Listen(IPAddress.Loopback, 44321, listenOptions =>
                      {
                          listenOptions.UseHttps("testcert.pfx", "ordinary");
                      });
                  }
                  else
                  {
                      options.Listen(IPAddress.Loopback, 5000);
                  }
              })
              .Build();
    }
}

}

1
simone.tino On

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.

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

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 setup

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

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

ASP.NET Core 2.0 setup

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

    }

}

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;

    }

If needed, you may setup HTTPS in a similar way.