Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 807
AMouat Avatar asked Jul 16 '19 16:07

AMouat


2 Answers

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.

like image 122
TValerii Avatar answered Oct 05 '22 12:10

TValerii


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

like image 35
Rye bread Avatar answered Oct 05 '22 14:10

Rye bread