55

I have a ASP.NET Core app. I run the application by running the command

dotnet run

I'm seeing the following error in one out of five situations when I build this ASP.NET Core app.

C:...\error CS2012: Cannot open 'C:...\bin\Debug\netcoreapp1.0\AAA.Web.dll' for writing -- 'The process cannot access the file 'C:...\bin\Debug\netcoreapp1.0\AAA.Web.dll' because it is being used by another process.'

In addition to the above issue, I also see no updates that I make in the CSHTML file. I have to stop the dotnet run command, build the app again and then run the dotnet run command.

How can I fix these issues?

enter image description here

14 Answers 14

28

This may also help when running your aspnetcore app in IIS.

Add the following to your csproj:

  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="echo &quot;App Offline&quot; /a &gt; &quot;$(ProjectDir)app_offline.htm&quot;" />
  </Target>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="del &quot;$(ProjectDir)app_offline.htm&quot;" />
  </Target>
5
  • For me, installing ".NET Core 2.2 Runtime & Hosting Bundle for Windows (v2.2.7)" solved the problems Sep 10, 2019 at 19:40
  • 3
    Update - problem is still there, wrong conclusion :-( Sep 17, 2019 at 10:01
  • 1
    Yeah problem has resurfaced for me as well.
    – csauve
    Sep 17, 2019 at 17:34
  • We are doing a file publish to an IIS test server. This answer didn't work as-is, but creating a simple batch file to write app_offline.htm to the deployment folder seems to have fixed it for me. Shame it has to be run manually
    – Rhumborl
    Mar 8, 2021 at 12:36
  • @FredrikStolpe For me, installing ".NET Core 2.2 Runtime & Hosting Bundle for Windows (v2.2.7)" didn't solve the problems
    – Emanuele
    May 6, 2021 at 10:36
23

If it is on local and application is running on IIS, then in windows task bar before your date time, there should be a IIS Express Option. select IIS Express, select exit. Try running application again.

exit IIS Express

21

In my case, I got the same issue due to dotnet watch run command even after stopping the watch mode.
Visual studio reported this error message : The file is locked by: ".NET core Host (pid)"
killing the process with the pid fixed the issue.

6
  • 20
    taskkill /f /pid 12345 Mar 7, 2019 at 22:18
  • Thanks @DonCheadle Great solution. Simple and does the trick! Can we vote for a comment to be the solution? I gave you a vote your comment at least.
    – Pegues
    Jun 26, 2019 at 12:32
  • mLar should include my comment in his answer Jun 26, 2019 at 18:19
  • This was the fix for me. The process was vs core host and visual studio reported the pid. Jun 26, 2019 at 18:33
  • You can also look for the process ID on Windows Resources Monitor's CPU tab and kill it from there. Nov 25, 2019 at 18:43
20

When trying to kill the process using /taskkill /f /pid <number in the error> it was telling me the process didn't even exist.

I simply had to find .NET Core Host in Task Manager and end it there.

This occasionally occurs when I edit a test project on Core 3.0 - not using IIS, it's a class library and a test project in a solution only.

enter image description here

2
  • 2
    I had a similar issue. The output mentioned the pid that had the test project locked, and I had to kill the process. It was "dotnet.exe" running (even after Visual Studio and everything on my desktop was closed).
    – Jason W
    Jan 8, 2020 at 19:30
  • 1
    This worked for me, although sometimes the ".NET Core Host" is placed under the main "Microsoft Visual Studio 2019" task, so just expand that and end the ".NET Core Host"
    – baltzar
    Feb 13, 2020 at 14:19
8

Open the Task Manager, find .NET Core Host, right-click, end task. enter image description here

0
6

The dotnet watch command can track file system changes and edits and compile them in to the running process. This is done by first installing the Microsoft.DotNet.Watcher.Tools package in the tools section of the project.json file. See documentation of its usage here.

Once it is installed, you can then run the app with the command dotnet watch run and make edits while avoiding the manual restarting of the application.

6

I ended up recycling the Application Pool to get my dlls unlocked. enter image description here

5

This has happened to me many times. The issue, at least in my case, has been that I have the application running. So when the compiler tries to update the dll it can't because the dll is locked and in use by the running application. Once I close the application and do the compile again, it works fine.

7
  • 5
    I don't think we should close the app and then re-compile every time we make a change. dotnet run somehow locking the dlls which should not be. Jan 24, 2017 at 13:52
  • 1
    That I don't know. I just know that closing the app will make the error go away.
    – RonC
    Jan 24, 2017 at 14:00
  • 1
    @wonderfulworld how do you expect changes to be applied if not by recompiling?
    – thehennyy
    Jan 24, 2017 at 14:21
  • Quit out of the app and then recompile, then the changes will be applied. If the app is running it has the dll locked. This works the same way for a WinForm app for example.
    – RonC
    Jan 24, 2017 at 14:23
  • I did not mean to say the changes should be displayed without re-compiling. What I meant is that I should not restart the app. Its like you set up an IIS app and point to your app. You don't have to restart the IIS website everytime you make a change in your code and re-compile. Jan 24, 2017 at 15:08
3

I may be a bit late to the party here, but for future people reading this question, it would appear that this is a bit of a 'feature' with how VS/core is able to interact with IIS and stop files from being locked:

https://developercommunity.visualstudio.com/content/problem/546858/bin-files-locked-by-iis-worker-process-1.html

It is apparently being investigated and looked into for .net core vnext.

2

I'm still running into this regularly in VS2017 and VS2019. Hopefully this helps someone.

This is what works every time for me.

If using IISExpress - Stop the Application using the icon in the task bar (near the clock/time) If using IIS - Stop and Restart the site OR In an Admin command prompt run taskkill -F -IM node.exe (Kills all node processes currently running)

2

100% Working For Me

it is being used by another process .net core

  1. go to task manager (for windows )
  2. Right click .NET Core Host
  3. click End Task as like see this image
1

This problem offen when you set AspNetCoreHostingModel to "OutOfProcess" then you have two solution for problem:

  1. change hosting model to "InProcess"
  2. use below command in CMD
taskkill /f /pid {.netcore hosting pid , show you in error line in visual studio}

For use that command, you do not use "" and {}.

0

For me, it worked by -

  1. Go to your bin and the environment, take a backup for files, try deleting the folder D:\CawProjects\EmailAnalytics\APIV1\APIV1\bin\Development if it does not allow you to delete and says Open/Used somewhere then follow the below process.

  2. Open task manager - find for .net core host and right-click to end the task

enter image description here

2
  • Every time I compile?
    – Emanuele
    May 6, 2021 at 10:38
  • does not happen to me every time. May 6, 2021 at 10:56
0

If you're using an IDE such as Rider, the .NET Hosts that you need to stop are tucked within the App in Task Manager:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.