dotnet publish Error: The process cannot access the file because it is being used by another process
Asked Answered
W

2

16

I get the following error when tring to run dotnet publish:

The process cannot access the file because it is being used by another process.

This appears to happen when you try to publish your app and you have been viewing the published version in the browser, for some reason it locks out the files.

Full error message:

C:\Program Files\dotnet\sdk\2.2.301\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(169,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp2.2\TemplateWebApp.dll" to "bin\Debug\netcoreapp2.2\publish\TemplateWebApp.dll". Beginning retry 1 in 1000ms. The process cannot access the file '\cpft-bi-web01\d$\Websites\TemplateWebApp\bin\Debug\netcoreapp2.2\publish\TemplateWebApp.dll' because it is being used by another process. [\cpft-bi-web01\d$\Websites\TemplateWebApp\TemplateWebApp.csproj]

I can resolve this by closing the browser and the restarting the website on IIS Manager but this is really not ideal, does anyone know the cause of this?

Welcher answered 16/7, 2019 at 16:58 Comment(3)
Are the target you publish to the same as the publish folder for your IIS website? ... If so, then have a look here (and check its comments about "web deploy"): #15277478Sudanic
And this, which might be a possible duplicate: https://mcmap.net/q/333136/-net-core-locking-filesSudanic
@LGSon yes the target I publish to and the folder IIS points to are the same, thanks I will look into web deploy.Welcher
S
11

To solve it, i recycle the apppool.

On local environment, i have a Post Build Event Project -> Properties -> Build Events -> Post-build event command line, that runs a .bat file that sits in project directory.

This is the code from bat file:

%SYSTEMROOT%\System32\inetsrv\appcmd recycle apppool /apppool.name:"YOUR APP POOL NAME"
dotnet publish --force --no-build
pause

Just make sure you replace YOUR APP POOL NAME with your actual pool name (and keep the double quotes)

So every time i build the solution, it recycles the pool and publishes the app. Because i just build the app (right before publishing), i specify --no-build to publish command.

Sixteenth answered 28/1, 2020 at 8:56 Comment(1)
This solution does not work for me. The output window says "MyAppPool successfully recycled" but I still get this error about the DLL being in use. Recycling the app pool manually fixes it, but this can't be a solution to have to recycle it every publish!Southsouthwest
O
4

I had the same error. Stopping the application pool on IIS, then publishing, then starting the pool again worked for me.

Ornithopter answered 24/11, 2020 at 8:40 Comment(1)
This answer led me to my solution. The site of mine that wouldn't publish wasn't even setup in IIS, but stopping the www service enabled publish to complete without error.Supervene

© 2022 - 2024 — McMap. All rights reserved.