14

I got following error while running dotnet build command on my .NET Core project.

C:\Program Files\dotnet\sdk\2.1.2\Microsoft.Common.CurrentVersion.targets(4106,5
 ): warning MSB3026: Could not copy "obj\Debug\netcoreapp2.0\Blog-Yantra.dll" to
 "bin\Debug\netcoreapp2.0\Blog-Yantra.dll". Beginning retry 1 in 1000ms. The proc
 ess cannot access the file 'E:\learn\blog\Blog-Yantra\bin\Debug\netcoreapp2.0\Bl
 og-Yantra.dll' because it is being used by another process.  [E:\learn\blog\Blog
 -Yantra\Blog-Yantra.csproj]

Screen shots of error message.

And my csproj file looks like:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <TypeScriptToolsVersion>2.3</TypeScriptToolsVersion>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.1" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />
  </ItemGroup>
</Project>
cederlof
  • 7,206
  • 4
  • 45
  • 62
Albert Einstein
  • 7,472
  • 8
  • 36
  • 71

9 Answers9

9

So, here I come up with the solution.
There was process running and was locking 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

Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
9

Most likely an open .Net core process running and blocking, if on windows, just kill it through the task manager.

enter image description here

Sgedda
  • 1,323
  • 1
  • 18
  • 27
2

Your ASP.Net application is running from the same directory, that is why you couldn't build your code. You have following options:

  1. Terminate the running ASP.Net application;
  2. Copy your code to another directory and build it from there; or
  3. Restart your computer and try building it again.

I would go with first option, but if you have difficulty finding out how to do it, last option is the easiest. In any case if for any reason you couldn't restart your computer, second one will surely work.

Sepehr Samiei
  • 382
  • 1
  • 6
2

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
cederlof
  • 7,206
  • 4
  • 45
  • 62
1

I have experienced the same issue when adding a new migration class. My Visual Studio (2017) instance was constantly freezing and when I restarted Visual Studio I started getting similar messages. What helped me was to kill the dotnet.exe process via Windows Task Manager.

  • dotnet.exe is showing up in Task Manager under "Background Processes" and ".Net Core Host". But this worked for me. – granadaCoder May 11 '20 at 17:39
1

The solution for me is to stop the IIS Express processes by right-clicking on the taskbar icon.

Or you can make a command line in "Pre-Build Event":

$(Solution)taskkill /FI "IMAGENAME eq iisexpress.exe" /F

Add this command in the "Properties" of your projet > "Build Events" Build Events

We don't have time to see if the command has been executed properly in "Ouput" view into "Build" section. Add this command line in "Pre-Build Event" to see the results:

cmd /c pause

You can remove this line after check...

A. Morel
  • 9,210
  • 4
  • 56
  • 45
0

A simple solution in VisualStudio Code is simply to kill the terminal.

enter image description here

Afterwards restart the PowerShell or CMD terminal either by starting a new terminal in the Terminal tab (Terminal - New Terminal) or by using the shortcut Ctrl + Shift + `

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Alex_P
  • 2,580
  • 3
  • 22
  • 37
0

For those like me who have this issue on a shared host with no access to anything whatsoever, don't try to copy your dll directly via panel or FTP, zip it then upload it then extract it this way it works in Plesk panel.

mohas
  • 1,911
  • 1
  • 16
  • 20
0

My error was related to a file read with netcore 3 and visual studio:

System.IO.IOException: The process cannot access the file 'C:\logs\api.log' because it is being used by another process.
   at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)

After 2 hours, only LockHunter worked!!

I downloaded it, installed, right click on the problematic file:

enter image description here

and search my file. After some seconds, the process was found:

enter image description here

Finally I click on "unclock it" and c# was able to read this file.


PD: Why windows do that? That's why modern developments don't use and will not use windows :D

JRichardsz
  • 14,356
  • 6
  • 59
  • 94