Iris Classon
Iris Classon - In Love with Code

ASPNET Core: killing a Kestrel session that refuses to die

I have a bad habit of using the NuGet Package Manager Console in Visual Studio as a PoSh CLI (which it sort of is), and today I ran ‘dotnet run’. And then I was unable to kill the session. The console was unresponsive, and so was the NuGet browser. Arghhh….ops. I restarted Visual Studio, and that got the console working again, but my port was taken and Kestrel spinning.

PowerShell has cmdlets for most things you could do in the good ol’ commandline, and there is one for working with TCP connection.

This will list all the connections:

 
Get-NetTCPConnection

and if you had some filtering with the parameters such as the port you can narrow down the result

 
Get-NetTCPConnection -Port 1106

The result is a list, and if you wrap the result in parenthesizes you can enumerate the items and pull out specific properties without piping+iteration

 
(Get-NetTCPConnection -LocalPort 1106).OwningProcess

This gave me the PID (process ID), and with that I could run

 
$rsult = (Get-NetTCPConnection -LocalPort 1106).OwningProcess[0]

Kill -Id $result

Voila. Process dead.

Comments

Leave a comment below, or by email.
Erik A. Brandstadmoen
4/30/2018 4:00:05 AM
This is a bit scary. I tried this, and got "4" as the process Id. Get-Process -Id 4 gives me the "System" process. I have ran the servers with Start-Process, not via Visual Studio, could that be the problem? 


Last modified on 2018-04-27

comments powered by Disqus