Developers targeting ASP.NET Core may come across a configuration issue when attempting to debug an application running on Kestrel on ASP.NET Core 2.1. Paul Ballard explains how to resolve it.
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:
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:
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:
For more information on configuring Kestrel for your ASP.NET Core 2.1 apps, you can read the documentation here.