Configuring Kestrel in ASPNET Core 21_870x220

I recently started up a new ASP.NET Core 2.1 project using the MVC starter project in macOS using the following command:

dotnet new mvc

The project gets created with no issues. I open up the folder in Visual Studio Code and hit F5 to make sure everything is working before I start adding my own code. But when I run the application, I get the following error:

configuration-1

After some digging around in my running processes and ports, I was able to find out that that port 5001 was in fact being used by Cisco AnyConnect VPN software. This is a VPN client used by many of my customers and not something I can just remove or kill off. Okay, so time to switch the ports for my ASP.NET Core app.

The most reasonable place you'd think to modify the endpoints for hosting your web app is in the launchSettings.json file. So, I made modifications there to change the ports out of the range used by Cisco. It then looked like this:

No problem, hit F5 and BAM! Same error. Strangely, if I run the application from the command line using dotnet run everything works great.

After some significant digging through the ASP.NET Core source code, I found that launchSettings.json file is not used by the debugger; it's actually part of the dotnet run command. Visual Studio Code doesn't use that command for debugging and so that file is ignored.

Fortunately, there are several ways to configure endpoints for Kestrel server. A common method is to add the .UseUrls() command to the WebHost configuration in Program.cs:

.UseUrls("http://locahost:50000","https://localhost:50001")

This will tell Kestrel to override the default settings with these URLs. However, this requires hard coding the values in the startup. Another option is to use the Kestrel configuration options in the appSettings.json file:

"Kestrel": {
  "Endpoints": {
    "Http": {
      "Url": "http://localhost:50000"
    },
    "HttpsDefaultCert": {
      "Url": "https://localhost:50001"
    }
  }
}

For more information on configuring Kestrel for your ASP.NET Core 2.1 apps, you can read the documentation here.


Paul Ballard
About the Author

Paul Ballard

Paul Ballard is a software architect and technologist with more than 30 years of experience leading teams and building software for small and large companies around the globe.  From award winning enterprise grade systems to mobile and cloud based solutions, he’s led teams in building the software that powers some of the best organizations in the world. Paul is also an avid supporter of the developer community and a former Microsoft MVP.

Related Posts

Comments

Comments are disabled in preview mode.