Search code examples
asp.net-corekestrel-http-server

Json configuration source ignored in Kestrel setup


I'm working with an ASP.NET Core 1 RTM web app and I'm updating Kestrel setup to latest conventions. Setup aims at having the following sources for server.urls, from lowest to highest priority:

  1. URLs set in Program.Main() code (default, e.g. for production)
  2. URLs set in hosting.Development.json (e.g. to override default while developing)
  3. URLs set in environment variables (e.g. to override default for staging or other production env.)

As per latest references (e.g. here on SO and here on Github), this is what I got now:

ProjDir\Program.cs:

public class Program
{
    // Entry point for the application
    public static void Main(string[] args)
    {
        const string hostingDevFilepath = "hosting.Development.json";
        const string environmentVariablesPrefix = "ASPNETCORE_";

        string currentPath = Directory.GetCurrentDirectory();

        var hostingConfig = new ConfigurationBuilder()
            .SetBasePath(currentPath)
            .AddJsonFile(hostingDevFilepath, optional: true)
            .AddEnvironmentVariables(environmentVariablesPrefix)
            .Build();

        System.Console.WriteLine("From hostingConfig: " +
            hostingConfig.GetSection("server.urls").Value);

        var host = new WebHostBuilder()
            .UseUrls("https://0.0.0.0")
            .UseConfiguration(hostingConfig)
            .UseKestrel()
            .UseContentRoot(currentPath)
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

ProjDir\hosting.Development.json:

{
    "server.urls": "http://localhost:51254"
}

From command line, having set ASPNETCORE_ENVIRONMENT=Development, this is the output:

> dotnet run

Project Root (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
From hostingConfig: http://localhost:51254
info: AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerMiddleware[0]
      An existing key was automatically added to the signing credentials list: <<yadda yadda yadda>>
Hosting environment: Development
Content root path: <<my project root dir>>
Now listening on: https://0.0.0.0:443
Application started. Press Ctrl+C to shut down.

My expected output would be instead Now listening on: http://localhost:51254. URLs value is correctly picked up from JSON source (as per console log), but then Kestrel configuration ignores that, even if UseConfiguration comes after UseUrls.

What am I missing? Thanks for your suggestions.


Solution

  • Try using urls instead of server.urls. The name of the setting changed post RC2.