Home > Software design >  dotnet publish fails intermittently because DLL is in use, even after recycling the app pool
dotnet publish fails intermittently because DLL is in use, even after recycling the app pool

Time:09-17

I am working on an ASP.NET Core 3.1 web app project and I noticed that the one-click web deploy (for which I have a folder profile setup) does not work because the DLLs in my website folder are in use by another process (probably w3wp.exe). I am still pretty new to ASP.NET Core but this was never a problem for me with regular ASP.NET. Anyway, I found that recycling the app pool fixes it temporarily, until 1-2 publishes later where it happens again.

I found a SO post that suggested having the project's post-build event recycle the app pool automatically before publishing, so I implemented that like this:

ECHO ON
%SYSTEMROOT%\System32\inetsrv\appcmd recycle apppool /apppool.name:"NeatF2P"
dotnet publish --output C:\Websites\NeatF2P --force --no-build
pause

Problem is, it doesn't always work. I build, I see "NeatF2P" successfully recycled in the output window, and still, I get the same error afterward. Here is a full output log:

Build started...
1>------ Build started: Project: NeatF2PWebsite, Configuration: Debug Any CPU ------
1>NeatF2PWebsite -> C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\bin\Debug\netcoreapp3.1\NeatF2PWebsite.dll
1>NeatF2PWebsite -> C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\bin\Debug\netcoreapp3.1\NeatF2PWebsite.Views.dll
1>"NeatF2P" successfully recycled
1>Microsoft (R) Build Engine version 16.9.0 57a23d249 for .NET
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 1 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 2 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 3 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 4 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 5 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 6 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 7 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 8 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 9 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): warning MSB3026: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Beginning retry 10 in 1000ms. The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): error MSB3027: Could not copy "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". Exceeded retry count of 10. Failed.  [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>C:\Program Files\dotnet\sdk\5.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(238,5): error MSB3021: Unable to copy file "obj\Debug\netcoreapp3.1\NeatF2PWebsite.dll" to "C:\Websites\NeatF2P\NeatF2PWebsite.dll". The process cannot access the file 'C:\Websites\NeatF2P\NeatF2PWebsite.dll' because it is being used by another process. [C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj]
1>Press any key to continue . . .
1>C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj(24,5): error MSB3073: The command "ECHO ON
1>C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj(24,5): error MSB3073: %SYSTEMROOT%\System32\inetsrv\appcmd recycle apppool /apppool.name:"NeatF2P"
1>C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj(24,5): error MSB3073: dotnet publish --output C:\Websites\NeatF2P --force --no-build
1>C:\Dev\Personal\NeatF2PWebsite\NeatF2PWebsite\NeatF2PWebsite.csproj(24,5): error MSB3073: pause" exited with code 1.
1>Done building project "NeatF2PWebsite.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

If I wait like a minute and then try the build again, sometimes it works, sometimes it doesn't. Recycling the app pool manually (in IIS) sometimes works, although it shouldn't be doing anything the command isn't, right? Why is this happening?

CodePudding user response:

recycling is like restarting it, it might acquire the handle to the file before another build try to get it, or not. stop it and restart it after publishing

CodePudding user response:

@L.Trabacchin's answer put me on the right track. If I modify my post-build event commands to stop and restart the app pool after the dotnet publish command:

ECHO ON
%SYSTEMROOT%\System32\inetsrv\appcmd stop apppool /apppool.name:"NeatF2P"
dotnet publish --output C:\Websites\NeatF2P --force --no-build
%SYSTEMROOT%\System32\inetsrv\appcmd start apppool /apppool.name:"NeatF2P"

This has yet to fail and I have done a dozen builds.

  • Related