Reputation: 34297
Edit
Process Explorer shows seven instances of w3wp with a lock on that file. So I think we need to stop these extra instances.
Since upgrading one project to .NET 5 (the solution has 27 projects in it), we're getting this error when publishing in the post-build event:
The process cannot access the file because it is being used by another process. [D:\somepath\MyProject.csproj]
The post-build event looks like this:
dotnet publish --no-build -p:PublishProfile=Properties\PublishProfiles\DeployToLocalIIS.pubxml
I've tried multiple solutions recommended online, including this:
dotnet build-server shutdown
dotnet publish --no-build -p:PublishProfile=Properties\PublishProfiles\DeployToLocalIIS.pubxml
And recycling the app pool:
%SYSTEMROOT%\System32\inetsrv\appcmd recycle apppool /apppool.name:"MyAppPool"
dotnet publish --no-build -p:PublishProfile=Properties\PublishProfiles\DeployToLocalIIS.pubxml
And stopping the app pool:
%SYSTEMROOT%\System32\inetsrv\appcmd stop apppool /apppool.name:"MyAppPool"
dotnet publish --no-build -p:PublishProfile=Properties\PublishProfiles\DeployToLocalIIS.pubxml
I've even tried putting an App_Offline.htm
file in the publish folder.
Links:
https://stackoverflow.com/a/51993805/279516
https://teamcity-support.jetbrains.com/hc/en-us/community/posts/360008187440-can-t-publish-file-due-to-file-in-use-by-IIS
But the same error happens anyway. Is there something else I can try?
Note: This didn't happen before upgrading one of the projects to .NET 5. But now the issue happens with the .NET Core 2.2 projects as well.
The output tab, in VS, shows this:
An error was encountered when processing operation 'Create File' on 'MyCompany.Common.dll'.
These are the links to the suggestions I tried above:
https://stackoverflow.com/a/59945099/279516
https://github.com/dotnet/core/issues/4210
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-build-server
https://www.thecodebuzz.com/netsdk1099-publishing-to-single-file-is-only-supported-for-executable/
Upvotes: 0
Views: 1411
Reputation: 34297
Stopping and starting the app pool fixed this issue. First, I had to reboot to clear all the w3wp suspended processes. Then I made my post-build events look like this, and things are good.
%SYSTEMROOT%\System32\inetsrv\appcmd stop apppool /apppool.name:"MyAppPool"
RD /S /Q "$(PublishDir)"
dotnet publish --no-build -p:PublishProfile=Properties\PublishProfiles\DeployToLocalIIS.pubxml
%SYSTEMROOT%\System32\inetsrv\appcmd start apppool /apppool.name:"MyAppPool"
Upvotes: 1