3
votes

I would like to setup HTTPS for my ASP.NET core application as well as defining different ports on which my app can listen to (e.g. a bit like what was shown by Scot Hanselman a while ago: https://channel9.msdn.com/Events/Build/2017/B8048).

I have checked the official resources:

I thought that the default builder was supposed to dig in the appsettings.json (or the appsettings.Development.json when it is relevant).

Program.cs content:

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

    public static IWebHost BuildWebHost(string[] args)
    {
        var builder = WebHost.CreateDefaultBuilder(args);
        return builder.UseStartup<Startup>().Build();
    }
}

appsettings.Development.json content:

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"
      }
    }
  },
  "Kestrel": {
    "Endpoints": {
      "Localhost": {
        "Address": "127.0.0.1",
        "Port": "5001"
      }
    }
  },
  "LocalhostwithHttps": {
    "Address": "127.0.0.1",
    "Port": "44333",
    "Certificate": {
      "Source": "Store",
      "StoreLocation": "LocalMachine",
      "StoreName": "My",
      "Subject": "CN=localhost"
    }
  }
}

Alas, when starting debugging against the development environment, my web app is ending up starting like this:

Hosting environment: Development
Content root path: c:\Users\ehoua\Desktop\There
Launching browser (cmd.exe /C start http://localhost:5000)
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

Do I have to really give up on the default builder and specify either the appsettings on which the the app needs to rely on or to hardcode the port values...?

[EDIT] Following @Set answer, I tried the following in Program.cs:

public static IWebHost BuildWebHost(string[] args)
{
    var developmentConfiguration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile($"appsettings.Development.json", optional: false)
    .Build();

    var productionConfiguration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile($"appsettings.json", optional: false)
    .Build();

    return WebHost.CreateDefaultBuilder(args)
    .UseEnvironment("Development").UseConfiguration(developmentConfiguration)
    .UseEnvironment("Production").UseConfiguration(productionConfiguration)
    .UseStartup<Startup>().Build();
}

I purposely set the optional argument to false in order to double check whether the appsettings file was properly added to the configuration. Also checked that the environment in the Startup Configure method was either Development or Production but still ending up with the same old port 5000 which belongs to none of my appsettings files.

[Edit] Possible in .NET 2.1

See: https://github.com/aspnet/KestrelHttpServer/issues/1290#issuecomment-353416087

1

1 Answers

3
votes

Looks like you need to call UseConfiguration directly to set an URL address read from appsettings.json:

public static IWebHost BuildWebHost(string[] args)
{
        var config = new ConfigurationBuilder()
            .AddJsonFile($"appsettings.Development.json", optional: true)
            .Build();

        return WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(config)
            .UseStartup<Startup>()
            .Build();
}

CreateDefaultBuilder method implementation uses ConfigureAppConfiguration method internally, not UseConfiguration. And there is a difference (from related Github issue):

The ConfigureAppConfiguration was intended to configure the IConfiguration in the application services whereas UseConfiguration is intended to configure the settings of the WebHostBuilder. Since the url address is a setting on the WebHostBuilder only UseConfiguration will work here.

Note that the WebHostBuilder's configuration is added to the application's configuration but the converse is not true; configuring the application's configuration does not affect the WebHostBuilder's configuration.


Instread of "Kestel.Endpoints" section your appsetting.json should simply have the following:

{
   ...
   "urls": "http://127.0.0.1:5001"
}