[英]How can I update ASP.NET Core app over an existing/running site without stopping the web server?
We have an ASP.NET Core site running on our test server that we would like to auto-deploy by XCopy to our IIS web server as we do our current apps, where I already have the site running.我们有一个 ASP.NET 核心站点在我们的测试服务器上运行,我们希望通过 XCopy 自动部署到我们的 IIS web 服务器,因为我们已经运行了我们当前的应用程序, I've added a publish profile that packages the site to a "publish-local" directory within the solution.我添加了一个发布配置文件,将站点打包到解决方案中的“本地发布”目录。 Whenever I try to copy over the existing site, all DLLs are being used by another process, presumably Kestrel, so I am forced to deploy to a sibling directory and re-map IIS to look at the sibling.每当我尝试复制现有站点时,所有 DLL 都被另一个进程(可能是 Kestrel)使用,因此我被迫部署到同级目录并重新映射 IIS 以查看同级。 How does one update a running ASP.NET Core site without having to manually intervene and stop either the Kestrel or IIS web servers?如何在无需手动干预和停止 Kestrel 或 IIS web 服务器的情况下更新正在运行的 ASP.NET 核心站点?
When running with IIS you can drop a file called app_offline.htm
(case sensitive) to your application folder.使用 IIS 运行时,您可以将名为app_offline.htm
(区分大小写)的文件拖放到应用程序文件夹中。 IIS will stop your application and will serve the contents of the app_offline.html
file. IIS 将停止您的应用程序并提供app_offline.html
文件的内容。 Now you can copy your application.现在您可以复制您的应用程序。 Once finished remove the app_offline.htm and IIS will start your app.完成后删除 app_offline.htm 和 IIS 将启动您的应用程序。 This is described in the docs and also in my post on running Asp.NET Core apps with IIS.这在文档和我关于使用 IIS 运行 Asp.NET Core 应用程序的帖子中进行了描述。
a little cheat we use is to rename the old files first (something like my.dll.old
), then copy over the new dlls.我们使用的一个小技巧是首先重命名旧文件(类似于my.dll.old
),然后复制新的 dll。 Then you can either force or wait for an app pool restart.然后,您可以强制或等待应用程序池重新启动。
Usually I take a backup of the existing version.通常我会备份现有版本。 Then I almost simultaneously recycle the app domain (if I'm using iis) and overwrite the entire contents of the root folder.然后我几乎同时回收应用程序域(如果我使用的是 iis)并覆盖根文件夹的全部内容。 This way the app restarts with the new version of the code.这样,应用程序将使用新版本的代码重新启动。 But it has to be super quick or else there can be issues.但它必须超级快,否则可能会出现问题。 In case if anything fails backup can be used to restore to original state.如果出现任何故障,可以使用备份恢复到原始状态。
You can switch the physical path of your IIS site/application with PowerShell:您可以使用 PowerShell 切换 IIS 站点/应用程序的物理路径:
($curPath = Get-WebFilePath -PSPath "IIS:\Sites\www.example.com\MyApp")
if ($curPath -like "*Blue*") {
Copy-Item -Path "D:\inetpub\wwwroot\MyAppPath\Staging\*" -Destination "D:\inetpub\wwwroot\MyAppPath\Green" -Recurse -Force
Set-ItemProperty IIS:\Sites\www.example.com\MyApp -name physicalPath -value "D:\inetpub\wwwroot\MyAppPath\Green"
} else {
Copy-Item -Path "D:\inetpub\wwwroot\MyAppPath\Staging\*" -Destination "D:\inetpub\wwwroot\MyAppPath\Blue" -Recurse -Force
Set-ItemProperty IIS:\Sites\www.example.com\MyApp -name physicalPath -value "D:\inetpub\wwwroot\MyAppPath\Blue"
}
Use a staging folder to which you copy your new published files.使用您将新发布的文件复制到其中的临时文件夹。 Then, the above script switches between the green and blue folder.然后,上面的脚本在绿色和蓝色文件夹之间切换。 You don't have to stop or recycle your app.您不必停止或回收您的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.