บางที dotnet watch run แล้วไม่คืน port

จาก Error เห็นว่า Port 5089 โดยยึดไำป

fail: Microsoft.Extensions.Hosting.Internal.Host[11]
      Hosting failed to start
      System.IO.IOException: Failed to bind to address http://127.0.0.1:5089: address already in use.
       ---> Microsoft.AspNetCore.Connections.AddressInUseException: Only one usage of each socket address (protocol/network address/port) is normally permitted.
       ---> System.Net.Sockets.SocketException (10048): Only one usage of each socket address (protocol/network address/port) is normally permitted.
         at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, Boolean disconnectOnFailure, String callerName)
         at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
         at System.Net.Sockets.Socket.Bind(EndPoint localEP)
         at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportOptions.CreateDefaultBoundListenSocket(EndPoint endpoint)
         at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
         --- End of inner exception stack trace ---

สำหรับวิธีแก้ Run Command ตามนี้เลยครับ

netstat -ano | findstr :[YOUR_PORT_NUMBER]
# OR PowerShell (returns PID only)
Get-NetTCPConnection -LocalPort [YOUR_PORT_NUMBER]| Select-Object -ExpandProperty OwningProcess

# ========================================
# Example
netstat -ano | findstr :5089
  TCP    127.0.0.1:5089         0.0.0.0:0              LISTENING       41944
  TCP    [::1]:5089             [::]:0                 LISTENING       41944
  TCP    [::1]:5089             [::1]:52154            TIME_WAIT       0
  TCP    [::1]:5089             [::1]:54681            TIME_WAIT       0
  TCP    [::1]:5089             [::1]:57245            TIME_WAIT       0
  TCP    [::1]:5089             [::1]:58437            TIME_WAIT       0
  TCP    [::1]:5089             [::1]:60189            TIME_WAIT       0
  TCP    [::1]:5089             [::1]:60458            TIME_WAIT       0
  TCP    [::1]:5089             [::1]:61170            TIME_WAIT       0
  TCP    [::1]:5089             [::1]:61934            TIME_WAIT       0
  TCP    [::1]:5089             [::1]:64160            TIME_WAIT       0

จากกนัั้นตอนนี้เราจะเจอแล้วว่า Process 41944 มันจอง Port ไว้อยู้่ ถ้าอยากรู้รายละเอียด Run คำสั่ง

tasklist /FI "PID eq <PID>"
# OR PowerShell
Get-Process -Id <PID>

# ========================================
# Example
Get-Process -Id 41944

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    423      60   170256     137844       4.52  41944   2 dotnet

และสุดท้าย Kill มันทิ้งด้วยคำสั่ง

taskkill /PID <PID> /F

# PowerShell
Stop-Process -Id <PID> -Force

# ========================================
# Example 
Stop-Process -Id 41944 -Force

เห็นว่ามันยาวยุบเป็นคำสั่งเดียวก็ได้นะ

Get-NetTCPConnection -LocalPort [YOUR_PORT_NUMBER] -ErrorAction SilentlyContinue | Select-Object -ExpandProperty OwningProcess | ForEach-Object { Stop-Process -Id $_ -Force }

# ========================================
# Example 
Get-NetTCPConnection -LocalPort 5089 -ErrorAction SilentlyContinue | Select-Object -ExpandProperty OwningProcess | ForEach-Object { Stop-Process -Id $_ -Force }


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.