Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Environment: 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.

With Core it doesn't do that. I need to stop and restart the dotnet MyApp.dll process.

How do I make it detect changes to binaries and reload?

like image 990
lonix Avatar asked Sep 06 '18 13:09

lonix


2 Answers

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. Uploads can pause or be slow and when uploading 50 files imagine restart after every single one every couple seconds. Server has no way to know when you have finished uploading last DLL. IIS has same problem, it's reliable in development because you refresh the page after the full DLL rebuild. 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. 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.

Then your publish workflow can be for example as following:

  1. Hit publish in Visual Studio
  2. VS will execute winscp script after publish is finished
    • 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
  3. Then your site is deployed, cleaned and restarted in the most reliable fashion with a single click.
like image 61
Xeevis Avatar answered Oct 20 '22 09:10

Xeevis


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. It's also a full-featured web server, and App Pool recycling on file changes is one of those features. Kestrel, which I assume your using is not. It's a very simple web server that does just what it needs to do as strictly a web server. 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.

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. Create a shell script to copy the bin directory and restart your app. 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.

like image 30
Chris Pratt Avatar answered Oct 20 '22 09:10

Chris Pratt