c# - Why setting env variables for ports such as ASPNETCORE_HTTP_PORT doesn't work in Visual Studio - Stack Overflow

admin2025-04-25  3

I have such a startup profile for my project in launcSettings.json.

"http": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "swagger",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ASPNETCORE_HTTP_PORTS": "8087",
    "ASPNETCORE_HTTPS_PORTS": "8088"
  }

But the application opens on port 5000. Why is this happening? When starting in docker-compose and specifying these environment variables, the application opens on the correct ports.

I have such a startup profile for my project in launcSettings.json.

"http": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "swagger",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ASPNETCORE_HTTP_PORTS": "8087",
    "ASPNETCORE_HTTPS_PORTS": "8088"
  }

But the application opens on port 5000. Why is this happening? When starting in docker-compose and specifying these environment variables, the application opens on the correct ports.

Share Improve this question edited Jan 16 at 16:12 Ivan Petrov 5,6072 gold badges11 silver badges24 bronze badges asked Jan 16 at 13:33 LepJakLepJak 133 bronze badges 9
  • This doesn't set the environment variables in docker, or anywhere else It's only used when working in with an IDE like VS to set the environment variables before starting the project – Panagiotis Kanavos Commented Jan 16 at 13:42
  • launchsettings.json as the docs explain isn't a startup profile or a .env file . when starting in docker-compose and specifying these variables, the application opens on the correct ports yes, because those are the actual environment variable settings. – Panagiotis Kanavos Commented Jan 16 at 13:49
  • 1 I don't understand you a bit. As I understand the operation of this mechanism. We are adding a variable to the environment. ASP.NET at startup, it reads environment variables and builds an application based on them. I understand that this works in Visual Studio. But logically, when I start the application from the IDE, it should start on the ports specified by me in this configuration, but for some reason it opens at 5000. – LepJak Commented Jan 16 at 13:51
  • No it shouldn't, because those aren't application configuration files at all. I suspect you misunderstand what environment variables are in the first place, no matter the OS. They aren't application settings, nor are they set by the application or any file. They're settings provided by the OS to the running application, set before the application even starts. The "environment" in question is the application execution environment specified by the OS, not a file. An application can modify the settings in its environment but no other execution will see those changes – Panagiotis Kanavos Commented Jan 16 at 13:55
  • 5000 is also non-standard, so it probably gets that value from launchsettings and isn't using the ones you've shown us. The default for .NET 8+ applications running in a container is to listen on port 8080. – Hans Kilian Commented Jan 16 at 13:55
 |  Show 4 more comments

1 Answer 1

Reset to default 1

It seems that Visual Studio forcibly sets ASPNETCORE_URLS when starting the application - see this question:

"ASPNETCORE_URLS": "https://localhost:5001;http://localhost:5000"

when applicationUrl and ASPNETCORE_URLS are not specified in the start profile (which is your case).

This interferes with setting the ports separately:

"ASPNETCORE_HTTP_PORTS": "8087",
"ASPNETCORE_HTTPS_PORTS": "8088"

When I run from Visual Studio 2022 under .NET 8 with your configuration I get:

warn: Microsoft.AspNetCore.Hosting.Diagnostics[15]
      Overriding HTTP_PORTS '8087' and HTTPS_PORTS '8088'. Binding to values defined by URLS instead 'https://localhost:5001/;http://localhost:5000/'.
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000

This doesn't happen when you use the same profile (e.g. named Dev) from the command line (or supply the env variables via Docker as your initial test)

dotnet run --launch-profile Dev

Output:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://[::]:8087
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://[::]:8088
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.

So the issue is Visual Studio. I am not sure how to turn off this behavior with a program setting, but the workaround I found answering the other question is to set ASPNETCORE_URLS to empty in launchSettings.json:

"environmentVariables": {
  "ASPNETCORE_ENVIRONMENT": "Development",
  "ASPNETCORE_URLS": "",
  "ASPNETCORE_HTTP_PORTS": "8087",
  "ASPNETCORE_HTTPS_PORTS": "8088"
}

I've tested this and it works for your case too on Visual Studio 2022 17.12 (.NET 8/ASP.NET 8)

转载请注明原文地址:http://anycun.com/QandA/1745529665a90816.html