.NET Core locking files
Asked Answered
V

14

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

Vespid answered 24/1, 2017 at 13:14 Comment(0)
I
30

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>
Intestine answered 30/12, 2018 at 22:53 Comment(5)
For me, installing ".NET Core 2.2 Runtime & Hosting Bundle for Windows (v2.2.7)" solved the problemsWolfgang
Update - problem is still there, wrong conclusion :-(Wolfgang
Yeah problem has resurfaced for me as well.Intestine
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 manuallyQuarterly
@FredrikStolpe For me, installing ".NET Core 2.2 Runtime & Hosting Bundle for Windows (v2.2.7)" didn't solve the problemsSwor
H
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

Hartwig answered 20/3, 2019 at 19:12 Comment(0)
C
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.

Cide answered 3/1, 2019 at 14:44 Comment(6)
taskkill /f /pid 12345Breughel
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.Grimsley
mLar should include my comment in his answerBreughel
This was the fix for me. The process was vs core host and visual studio reported the pid.Outset
You can also look for the process ID on Windows Resources Monitor's CPU tab and kill it from there.Kincardine
This is exactly what I'm looking for.Recurrent
L
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

Lenrow answered 5/12, 2019 at 2:5 Comment(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).Borlase
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"Pagano
P
8

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

Pacification answered 4/3, 2020 at 13:25 Comment(0)
P
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.

Precede answered 24/1, 2017 at 14:43 Comment(0)
J
6

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

Jea answered 15/2, 2019 at 13:51 Comment(0)
L
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.

Locale answered 24/1, 2017 at 13:44 Comment(7)
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.Vespid
That I don't know. I just know that closing the app will make the error go away.Locale
@wonderfulworld how do you expect changes to be applied if not by recompiling?Suez
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.Locale
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.Vespid
dotnet watch run did not help. I get the same error often.Vespid
My app doesn't actually close when I exit the command window, I have to hunt it down in Task Manager to get the process to shut off. Very annoying.Mandell
D
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.

Desirous answered 10/9, 2019 at 13:50 Comment(0)
M
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)

Maguire answered 24/10, 2019 at 3:36 Comment(0)
D
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
Driftwood answered 18/8, 2020 at 11:23 Comment(0)
F
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 {}.

Front answered 10/10, 2020 at 8:28 Comment(0)
S
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

Seaplane answered 19/3, 2021 at 5:0 Comment(2)
Every time I compile?Swor
does not happen to me every time.Seaplane
D
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

Disentwine answered 3/3, 2023 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.