Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure endpoints in Kestrel?

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();
like image 533
Exevan Avatar asked Oct 22 '17 20:10

Exevan


People also ask

How do I change the port on my Kestrel?

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.

Can we host net core application in Kestrel?

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.

Which of the following features are supported Kestrel?

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.

Does Kestrel need reverse proxy?

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)


2 Answers

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:

enter image description here

to this:

enter image description here

(PIdea being the name of my project)

like image 161
Exevan Avatar answered Sep 20 '22 01:09

Exevan


Add

"Kestrel": {
  "EndPoints": {
    "Http": {
      "Url": "http://localhost:5002"
    },
    "Https": {
      "Url": "https://localhost:5003"
    }
  }
}  

to appsettings.json.

like image 42
amilamad Avatar answered Sep 19 '22 01:09

amilamad