Visual Studio 2022 Publish Fails – Unable to delete files

I am trying to Publish a sample DotNetCore project (from the basic VueJs/Core solution template) to an IIS server with the ‘Delete Existing Files’ option. If the app has been running, then the Publish throws errors because of files that it cannot delete. Fortunately, a second Publish succeeds as the first attempt shuts the app down and allows the files to be deleted.

<Target Name="AddAppOffline" BeforeTargets="Publish">
   <Message Text="Copying app_offline.htm" Importance="high" />
   <Copy SourceFiles="app_offline.htm" DestinationFolder="$(PublishDir)" />
   <Message Text="Completed Copying app_offline.htm" Importance="high" />
</Target>

<Target Name="RemoveAppOffline" AfterTargets="Publish">
   <Message Text="Deleting app_offline.htm" Importance="high" />
   <Delete Files="$(PublishDir)app_offline.htm" />
</Target>

I am using the code above in the .csproj file that should add the app_offline.htm file into Publish folder to shut the app pool down. Despite the order shown in the VS Output window, I do not see the file being added before the Delete errors are thrown.

Build started at 1:18 AM...
1>------ Build started: Project: vuecore2024.client, Configuration: Release Any CPU ------
2>------ Build started: Project: VueCore2024.Server, Configuration: Release Any CPU ------
2>VueCore2024.Server -> 
C:GitSamples2024VueCore2024VueCore2024.Server
binReleasenet8.0VueCore2024.Server.dll
3>------ Publish started: Project: VueCore2024.Server, Configuration: Release Any CPU ------
Connecting to C:tempVueCore2024...
VueCore2024.Server -> C:GitSamples2024VueCore2024VueCore2024.Server
binReleasenet8.0VueCore2024.Server.dll
npm run build

> [email protected] build
> vite build

[36mvite v5.2.11 [32mbuilding for production...[36m[39m
transforming...
[32mΓ£ô[39m 23 modules transformed.
rendering chunks...
computing gzip size...
[2mdist/[22m[32mindex.html                 
[39m[1m[2m 0.43 kB[22m[1m[22m[2m Γöé gzip:  0.29 kB[22m
[2mdist/[22m[35massets/index-BXAxuANx.css  
[39m[1m[2m 3.73 kB[22m[1m[22m[2m Γöé gzip:  1.22 kB[22m
[2mdist/[22m[36massets/index-X070Lzgl.js   
[39m[1m[2m64.83 kB[22m[1m[22m[2m Γöé gzip: 25.70 kB[22m
[32mΓ£ô built in 547ms[39m

Copying app_offline.htm
Completed Copying app_offline.htm

VueCore2024.Server ->     C:GitSamples2024VueCore2024VueCore2024.Server     
objReleasenet8.0PubTmpOut
C:Program Filesdotnetsdk8.0.204SdksMicrosoft.NET.Sdk.Publish
targetsPublishTargetsMicrosoft.NET.Sdk.Publish.FileSystem.targets(63,5): 

Error  MSB3061: Unable to delete file     
"C:tempVueCore2024Microsoft.AspNetCore.OpenApi.dll". Access to the path 
'C:tempVueCore2024Microsoft.AspNetCore.OpenApi.dll' is denied.
C:Program Filesdotnetsdk8.0.204SdksMicrosoft.NET.Sdk.Publish
targetsPublishTargetsMicrosoft.NET.Sdk.Publish.FileSystem.targets(63,5): 

Error MSB3061: Unable to delete file 
"C:tempVueCore2024Microsoft.OpenApi.dll". 
Access to the path 'C:tempVueCore2024Microsoft.OpenApi.dll' is denied.
C:Program Filesdotnetsdk8.0.204SdksMicrosoft.NET.Sdk.Publish
targetsPublishTargetsMicrosoft.NET.Sdk.Publish.FileSystem.targets(63,5):

Error MSB3061: Unable to delete file 
"C:tempVueCore2024Swashbuckle.AspNetCore.Swagger.dll". Access to the path 
'C:tempVueCore2024Swashbuckle.AspNetCore.Swagger.dll' is denied.
 C:Program Filesdotnetsdk8.0.204SdksMicrosoft.NET.Sdk.Publish
targetsPublishTargetsMicrosoft.NET.Sdk.Publish.FileSystem.targets(63,5):

Error MSB3061: Unable to delete file 
"C:tempVueCore2024Swashbuckle.AspNetCore.SwaggerGen.dll". Access to the path 
'C:tempVueCore2024Swashbuckle.AspNetCore.SwaggerGen.dll' is denied.
C:Program Filesdotnetsdk8.0.204SdksMicrosoft.NET.Sdk.Publish
targetsPublishTargetsMicrosoft.NET.Sdk.Publish.FileSystem.targets(63,5):

Error MSB3061: Unable to delete file 
"C:tempVueCore2024Swashbuckle.AspNetCore.SwaggerUI.dll". Access to the path 
'C:tempVueCore2024Swashbuckle.AspNetCore.SwaggerUI.dll' is denied.
C:Program Filesdotnetsdk8.0.204SdksMicrosoft.NET.Sdk.Publish
targetsPublishTargetsMicrosoft.NET.Sdk.Publish.FileSystem.targets(63,5): 

Error MSB3061: Unable to delete file "C:tempVueCore2024VueCore2024.Server.dll". 
Access to the path 'C:tempVueCore2024VueCore2024.Server.dll' is denied.

3>Build failed. Check the Output window for more details.
========== Build: 2 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 1:18 AM and took 03.698 seconds ==========
========== Publish: 0 succeeded, 1 failed, 0 skipped ==========
========== Publish completed at 1:18 AM and took 03.698 seconds ==========

The path to the app_offline.htm file is working correctly as the file is there at the end of the publish if the Delete Target step is removed. My guess is that the timing is somehow off with the deletions happening before the App Pool shutdown is complete.

Any suggestion as to how to make this work on the first publish, preferably without having to resort to a external Powerscript file or something more elaborate?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật