此解決方法來源於微軟官方
原文地址:https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-net-6-preview-3/#shadow-copying-in-iis
項目在發布的時候,根目錄會產生一個web.config文件,修改此文件為
原文如下
我們在 IIS ASP.NET 核心模塊中添加了一項新功能,以添加對卷影復制應用程序程序集的支持。目前,.NET 在 Windows 上運行時會鎖定應用程序二進制文件,因此當應用程序仍在運行時,無法替換二進制文件。雖然我們的建議仍然是使用應用脫機文件,但我們認識到在某些情況下(例如 FTP 部署)無法執行此操作。
在這種情況下,可以通過自定義 ASP.NET 核心模塊處理程序設置來啟用卷影復制。在大多數情況下,ASP.NET 核心應用程序沒有簽入到源代碼管理中的 web.config,您可以對其進行修改(它們通常由 SDK 生成)。可以添加此示例以開始使用。
web.config
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- To customize the asp.net core module uncomment and edit the following section. For more info see https://go.microsoft.com/fwlink/?linkid=838655 --> <system.webServer> <handlers> <remove name="aspNetCore"/> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModulev2" resourceType="Unspecified"/> </handlers> <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout"> <handlerSettings> <handlerSetting name="experimentalEnableShadowCopy" value="true" /> <handlerSetting name="shadowCopyDirectory" value="../ShadowCopyDirectory/" /> <!-- Only enable handler logging if you encounter issues--> <!--<handlerSetting name="debugFile" value=".\logs\aspnetcore-debug.log" />--> <!--<handlerSetting name="debugLevel" value="FILE,TRACE" />--> </handlerSettings> </aspNetCore> </system.webServer> </configuration>
注意:processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" 要更改為
processPath="dotnet" arguments=".\GamePlayer.Web.dll"
GamePlayer.Web.dll 為你項目的DLL名稱
../ShadowCopyDirectory/ 也可以更改為你自己存放副本的目錄
否則會報錯
已測試,好用
.net 7 中有改動
https://weblog.west-wind.com/posts/2022/Nov/07/Avoid-WebDeploy-Locking-Errors-to-IIS-with-Shadow-Copy-for-ASPNET-Core-Apps
<aspNetCore processPath=".\Westwind.Webstore.Web.exe" hostingModel="inprocess" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" > <handlerSettings> <handlerSetting name="enableShadowCopy" value="true" /> <handlerSetting name="shadowCopyDirectory" value="../ShadowCopyDirectory/" /> </handlerSettings> </aspNetCore>