asp.net-corevisual-studio-code

ASP.NET Core ASPNETCORE_HTTP_PORTS always overridden in VS Code debugger


I'm debugging an ASP.NET Core application in VS Code. My project has the following launchsettings.json:

{
  "profiles": {
    "Development": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_HTTP_PORTS": "5003"
      }
    }
  }
}

which should set the port to 5003. Instead, when debugging, I get the message:

Overriding HTTP_PORTS '5003' and HTTPS_PORTS ''. Binding to values defined by URLS instead 'https://localhost:5001/'

Where is this value 5001 coming from?

If I use dotnet run my application uses the port 5003 as desired, it's only when debugging through VS Code that this other setting seems to override it, but I have no idea where this could be coming from.

Does VS Code inject env variables into the debugging session?


Solution

  • Does VS Code inject env variables into the debugging session?

    Yes you are right, VS Code does inject environment variables into the debugging session, and this can override settings defined in launchSettings.json.

    When you debug an ASP.NET Core application in VS Code, the debugger often uses launch.json settings, which can override the launchSettings.json file. VS Code may also automatically inject certain environment variables, including ASPNETCORE_URLS, which dictates the URL(s) your application should bind to. If ASPNETCORE_URLS is set, it will override the port settings from ASPNETCORE_HTTP_PORTS.

    Where is this value 5001 coming from?

    Well, this port typically written in the file which resides in .vscode directory of your project. You can add the environment variable to ensure that the correct port is used during debugging.

    Note: Please refer to this official document for additional details.