Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

c# - Kestrel port is always in use

I'm using VSCode to develop an ASP.NET Core 5.0 application on Docker. I'm using the default Kestrel webserver behing an Nginx reverse proxy.

When I want to run the app, using docker-compose build and docker-compose up the app runs with a message saying that deployed apps cannot be run in development environment. Alright so I tried to run the app in debug with VSCode.

The problem is that whichever port I specify in the config, Kestrel will shoot an error saying

Failed to bind to address http://127.0.0.1:*port*: address already in use.

Name a port, any port, it will give an error. Given that it kinda works in docker-compose mode, I suspect the tasks.json file, or maybe the app is double-launched.

I ensured that all containers were stopped before launching in debug. I even did a docker system prune -a to start with a fresh environment. Nothing take this error out of the way.

Here are some config files :

launch.json

"preLaunchTask": "docker-run: debug"

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/app/app.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "docker-build: debug",
            "type": "docker-build",
            "dependsOn": [
                "build"
            ],
            "dockerBuild": {
                "tag": "appnet_app:dev",                
                "target": "base",
                "dockerfile": "${workspaceFolder}/app/Dockerfile",
                "context": "${workspaceFolder}/app",
                "pull": true
            },
            "netCore": {
                "appProject": "${workspaceFolder}/app/app.csproj"
            },
            "options": {            
                "env": {
                    "ASPNETCORE_ENVIRONMENT": "Development"
                }
            }
        },
        {
            "label": "docker-run: debug",
            "type": "docker-run",
            "dependsOn": [
                "docker-build: debug" 
            ],
            "dockerRun": {
                "env": {
                    "ASPNETCORE_ENVIRONMENT":"Development" 
                },
                "image": "appnet_app:dev"
            },
            "netCore": {
                "appProject": "${workspaceFolder}/app/app.csproj",
                "enableDebugging": true
            },
            "options": {
                "env": {
                    "ASPNETCORE_ENVIRONMENT": "Development"
                }
            }
        }
    ]
}

app Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim as base
 
WORKDIR /app
COPY bin/Debug/net5.0 .
 
ENV ASPNETCORE_URLS=http://+:8000
EXPOSE 8000
 
ENTRYPOINT ["dotnet", "app.dll"]

I also have a launchSettings.json in Properties directory, which I'm quite confused which file is used between this one and the launch.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:4324",
      "sslPort": 5356
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "app": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:8000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Program.cs

public static void Main(string[] args)
{
    CreateHostBuilder(args).Build().Run();   // <----- Crashes on this line
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();                    
            webBuilder.UseUrls("http://localhost:8000");
        });

question from:https://stackoverflow.com/questions/65861998/kestrel-port-is-always-in-use

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...