I'm learning how to work with in ASP.NET Core 2 and I've come across a rather annoying problem. Whenever I run my application, the Kestrel server ignores my endpoint configuration and instead starts listening on a random port. Needless to say, this is not the behavior I expected. When digging through the server logs I also came upon this message:
Overriding endpoints defined in UseKestrel() because PreferHostingUrls is set to true. Binding to address(es) 'http://localhost:<some random port>' instead.
I have so far been unable to find any documentation on this "PreferHostingUrls" setting or how to change it. Does anyone know how i can configure Kestrel to listen on the specified port instead of a random one?
My host configuration looks like this:
WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
.UseKestrel(
options =>
{
options.Listen(IPAddress.Loopback, 50734);
options.Listen(IPAddress.Loopback, 44321, listenOptions =>
{
listenOptions.UseHttps("pidea-dev-certificate.pfx", "****************");
});
})
.UseStartup<Startup>()
.UseIISIntegration()
.Build();
You can set the Kestrel ports inside the appsettings. json file. A simple block of JSON does it. You might notice that there is an appsettings.Development.
Kestrel is a cross-platform web server for ASP.NET Core. Kestrel is the web server that's included and enabled by default in ASP.NET Core project templates. Kestrel supports the following scenarios: HTTPS.
Kestrel supports cross-platform, which is nothing but the ability to run on multiple platforms (Windows/Linux\Mac servers). Also, it can be run on multiple servers such as Nginx and Apache. When Kestrel is not being used, then for each server, the startup file of the code has to be modified as per the server's needs.
Kestrel can be used by itself or with a reverse proxy server. A reverse proxy server receives HTTP requests from the network and forwards them to Kestrel. Examples of a reverse proxy server include: Internet Information Services (IIS)
Ok, so it turned out IISExpress was the culprit here.
For some reason, the default build configuration of Visual Studio 2017 starts my app on an IISExpress server, which does not listen to my endpoint configuration. To solve the issue, I simply had to switch to a custom run configuration.
To summary, I just had to switch from this:
to this:
(PIdea being the name of my project)
Add
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://localhost:5002"
},
"Https": {
"Url": "https://localhost:5003"
}
}
}
to appsettings.json
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With