Error the process cannot access the file because it is being used by another process while building project by CLI on .NET Core

So, here I come up with the solution.
There was process running and lock assemblies, since I did dotnet run to run the project through dotnet cli and I did Ctrl + c to terminate the running process. But Ctrl + c didn't killed the all process and Childs continue running and lock assemblies i.e kestrel server was still running on the same port.

To kill the running kestrel server I had run following command.

C:\Users\Kiran>netstat -ano -p TCP | find /I "listening" | find /I "2492"
TCP    127.0.0.1:2492         0.0.0.0:0              LISTENING       820

C:\Users\Kiran>taskkill /F /PID 820
SUCCESS: The process with PID 820 has been terminated.

command you need to run

netstat -ano -p TCP | find /I "listening" | find /I "{your port number}"
taskkill /F /PID {your process ID}

How to manually stop kestrel server

Some references for this issue are:

How to manually stop kestrel server. Question asked on SO

Port localhost:5000 not released causing error System.IO.IOException: Failed to bind to address http://127.0.0.1:5000: address already in use. issue posted on github

terminating dotnet run doesn't terminate child. Issue on github

Windows: How to kill process by port. Blog post


If your web server is IIS you can put an empty file named app_offline.htm in the project root, which will gracefully shut down the app.

I have it scripted like so:

  1. Add app_offline.htm to project root hosted by IIS
  2. dotnet build
  3. Remove app_offline.htm

Tags:

C#

.Net

.Net Core