[SOLVED] How can I configure endpoints in Kestrel?

Issue

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();

Solution

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)

Answered By – Exevan

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *