Can I set listen URLs in appsettings.json in ASP.net Core 2.0 Preview?
Asked Answered
C

5

18

I'm creating an ASP.net Core 2.0 app to run on the .net Core 2.0 runtime, both currently in their Preview versions. However, I cannot figure out how to have Kestrel use something other than the default http://localhost:5000 listen URL.

Most documentation that I could Google talks about a server.urls setting, which seems to have been changed even in 1.0-preview to just be urls, however neither works (turning on Debug logging has Kestrel telling me that no listen endpoints are configured).

A lot of documentation also talks about a hosting.json and that I can't use the default appsettings.json. However, if I compare the recommended approach of loading a new config, this looks pretty much exactly like what the new WebHost.CreateDefaultBuilder method does, except that it loads appsettings.json.

I currently don't understand how appsettings.json and IConfigureOptions<T> are related, if at all, so it's possible that my trouble stems from a lack of understanding of what KestrelServerOptionsSetup actually does.

Cacique answered 22/5, 2017 at 16:26 Comment(1)
I found that appsettings.json can be used to set urls, see here: #46696615Exhibitor
K
21

I got it working with this

public static IWebHost BuildWebHost(string[] args) => 
        WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("hosting.json", optional: true)
                .Build()
            )
            .UseStartup<Startup>()
            .Build();

And the hosting.json

{ "urls": "http://*:5005;" }
Kickoff answered 22/5, 2017 at 17:14 Comment(2)
Thank you. I misunderstood that CreateDefaultBuilder uses ConfigureAppConfiguration to load appsettings.json, which is different from UseConfiguration.Cacique
I'm using .NET 5 and when I create a new MVC app there isn't a hosting.json file. Should I create it or is this an outdated way of settings the URLs?Aqaba
E
25

To set listen URLs in appsettings.json, add "Kestrel" section:

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

Reference: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2

Eudosia answered 21/1, 2019 at 17:14 Comment(3)
Does it affect deployment on a server instance? How is the priority or order if I also set an ASPNETCORE_URLS in my service config?Hermann
Looks like ASPNETCORE_URLS (environment variable) has higher precedence than appsettings.json - See devblogs.microsoft.com/premier-developer/…Eudosia
Worked for me too. Thank you. I wanted Kestrel to use a dynamic port and then I wanted to programmatically get that port. "Url": "http://*:0" did it.Monopoly
K
21

I got it working with this

public static IWebHost BuildWebHost(string[] args) => 
        WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("hosting.json", optional: true)
                .Build()
            )
            .UseStartup<Startup>()
            .Build();

And the hosting.json

{ "urls": "http://*:5005;" }
Kickoff answered 22/5, 2017 at 17:14 Comment(2)
Thank you. I misunderstood that CreateDefaultBuilder uses ConfigureAppConfiguration to load appsettings.json, which is different from UseConfiguration.Cacique
I'm using .NET 5 and when I create a new MVC app there isn't a hosting.json file. Should I create it or is this an outdated way of settings the URLs?Aqaba
T
6

Works for me as it used to be

WebHost.CreateDefaultBuilder(args)
    .UseConfiguration( new ConfigurationBuilder().AddCommandLine(args).Build() )
    .UseStartup<Startup>()
    .Build();

Then

dotnet myapp.dll --urls "http://*:5060;"
Thole answered 7/7, 2017 at 1:20 Comment(0)
R
2

None of the above worked for me. This one worked for me:

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
            .UseKestrel(options =>
            {
                options.Listen(System.Net.IPAddress.Loopback, 44306, listenOptions =>
                {
                    listenOptions.UseHttps("mysertificate.pfx", "thecertificatePassword");
                });
            })
        .Build();

(Change the 44306 to a port of your own liking)

And there might be a lot of help in this StackOverflow answer

Repudiate answered 12/3, 2018 at 4:4 Comment(0)
F
0

None of these solutions really worked for me here in March 2024 running net8.0 and a minimal WebAPI with top-level statements.

I finally discovered code that I could add to my Program.cs that worked great:

var portNumber = 5270;
builder.WebHost.ConfigureKestrel((context, serverOptions) =>
{
    serverOptions.Listen(System.Net.IPAddress.Loopback, portNumber);
});

This set my app running on 127.0.0.1:5270.

Fantasm answered 7/3, 2024 at 13:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.