简体   繁体   English

dll文件更改时重新加载ASP.NET Core应用(bin部署)

[英]Reload ASP.NET Core app when dll files change (bin deploy)

Environment: ASP.NET Core 2.1, Ubuntu. 环境:ASP.NET Core 2.1,Ubuntu。

In old style ASP.NET, when I did a bin deploy (uploaded some dll files for example), the webapp would detect that and reload itself - very useful. 在旧式ASP.NET中,当我执行bin部署(例如,上载了一些dll文件)时,webapp会检测到该情况并重新加载自身-非常有用。

With Core it doesn't do that. 使用Core并不能做到这一点。 I need to stop and restart the dotnet MyApp.dll process. 我需要停止并重新启动dotnet MyApp.dll进程。

How do I make it detect changes to binaries and reload? 如何使它检测二进制文件的更改并重新加载?

There are file watchers in Ubuntu that can issue restart commands whenever files are changed like systemd or inotify , but I would strongly advise against that. Ubuntu中有文件监视程序,每当文件更改时,例如systemdinotify ,它们都可以发出重新启动命令,但是我强烈建议您这样做。 Uploads can pause or be slow and when uploading 50 files imagine restart after every single one every couple seconds. 上载可能会暂停或变慢,并且上载50个文件时,每隔几秒钟就会重新启动一次。 Server has no way to know when you have finished uploading last DLL. 服务器无法知道何时完成上一个DLL的上传。 IIS has same problem, it's reliable in development because you refresh the page after the full DLL rebuild. IIS具有相同的问题,它在开发中是可靠的,因为在完全重建DLL之后刷新页面。 But when in production you don't want random visitors to boot your site midway while it's still uploading. 但是,在生产中,您不希望随机访问者在您的网站仍在上传时中途启动它。 Errors, file locks, all kinds of weirds things can happen. 错误,文件锁定,各种奇怪的事情都可能发生。

As pointed by Chris Pratt you want to script your deployment workflow. 正如Chris Pratt指出的那样,您想要编写部署工作流程的脚本。 Idk what environment you are developing on, but if you have Visual Studio and WinSCP it's as easy as writing couple lines of code with Scripting and Task Automation . 知道您正在开发什么环境,但是如果您拥有Visual Studio和WinSCP,则就像使用Scripting和Task Automation编写几行代码一样容易。

Then your publish workflow can be for example as following: 然后,您的发布工作流可以如下所示:

  1. Hit publish in Visual Studio 点击在Visual Studio中发布
  2. VS will execute winscp script after publish is finished 发布完成后,VS将执行winscp脚本
    • Authenticate on remote server 在远程服务器上进行身份验证
    • Upload publish folder to a remote folder 将发布文件夹上载到远程文件夹
    • Remove old files 删除旧文件
    • Prune logs 修剪日志
    • After all done issue systemctl restart kestrel-myapp command 完成所有操作后, systemctl restart kestrel-myapp命令
  3. Then your site is deployed, cleaned and restarted in the most reliable fashion with a single click. 然后,单击即可以最可靠的方式部署,清理和重新启动您的站点。

There's nothing I'm aware of that will do this for you. 我所知道的没有什么可以为您做到的。 IIS watches things like the bin directory, web.config, etc. and recycles the App Pool when it detects changes, but that's because it knows to. IIS监视bin目录,web.config等内容,并在检测到更改时回收应用程序池,但这是因为它知道。 It's also a full-featured web server, and App Pool recycling on file changes is one of those features. 它也是功能齐全的Web服务器,对文件更改进行回收的App Pool是这些功能之一。 Kestrel, which I assume your using is not . 红est,我假设您不是 It's a very simple web server that does just what it needs to do as strictly a web server. 这是一个非常简单的Web服务器,可以像严格意义上的Web服务器一样执行所需的操作。 That's why a more traditional web server like IIS, Apache, Nginx, etc. is normally used as a reverse proxy in front of Kestrel - to provide more advanced functionalities. 这就是为什么更传统的Web服务器(如IIS,Apache,Nginx等)通常用作Kestrel前面的反向代理-提供更多高级功能。

All that said, though, this is really just a matter of your release strategy. 尽管如此,这实际上只是您的发布策略的问题。 Personally, I'd encourage you to go with something far more robust that copy-pasting DLLs, but if you want to go that route, you can also script it. 就个人而言,我鼓励您使用比复制粘贴DLL更健壮的功能,但是如果您要采用这种方式,也可以编写脚本 Create a shell script to copy the bin directory and restart your app. 创建一个Shell脚本以复制bin目录重新启动您的应用程序。 Your release should be one rails as much as possible. 您的发布应尽可能地是一条轨道。 Every time human intervention is called for, you have a potential point of failure, because humans are inherently fallible. 每次需要人工干预时,您都有潜在的失败点,因为人类天生就容易犯错。 A script, however, once tested and ensured to work, will pretty much work every time, because it always does the same things in the same order. 但是,脚本一旦经过测试并确定可以工作,则每次都会工作很多,因为它总是以相同的顺序执行相同的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM