davidfowl / WebApplicationPlugins

A sample plugin model for ASP.NET Core applications
119 stars 13 forks source link

Is it possible to load and unload plugins on the fly? #4

Closed karayakar closed 2 years ago

karayakar commented 2 years ago

Wondering is it possible to load and unload plugins on the fly, without restarting app?

InDieTasten commented 2 years ago

@karayakar The sample isn't demonstrating this scenario at the moment, but it wouldn't be too difficult to add such functionality.

However, unloading is a process that needs to be somewhat coordinated between host and plugin, as the AssemblyLoadContext doesn't support forceful unload.

There is a bunch of information written about unloadability right here: https://github.com/dotnet/runtime/blob/main/docs/design/features/unloadability.md

Edit: I have a sample using the ALC to repeatedly load and unload a plugin in this repo: https://github.com/InDieTasten/DevTease.Plugins (keep in mind, that code is 4 years old, and it doesn't integrate with ASP.NET Core as Davids sample right here is doing, so when it comes to routes and services with ASP.NET Core, these need to be handled as part of the unload in some way. As mentioned before, this would be something that needs to be "coordinated" in some way to play along nicely)

karayakar commented 2 years ago

@InDieTasten Thank you, that would be great if we can add in to this sample. Previously I was doing some POC but had a lot of issues with plugin dependencies like same assembly different versions in plugins were big problem. Unloading was another big issue. (I was trying to create real-time continuous development for the plugins) Thank you again.

InDieTasten commented 2 years ago

@karayakar I could imagine a setup where all of these registrations with ASP.NET Core are also properly unregistered when unloading a plugin, and the host having some kind of file-watcher on plugin assemblies to reload during development. The actually loaded assembly probably needs to be copied into a different directory or renamed beforehand, as loading will put a lock on the file preventing future builds from overriding the assembly.

davidfowl commented 2 years ago

Unloading is always tricky. Building a truly dynamic system with unloading and reloading is doable but making it reliable is difficult. So the answer is yes but I haven't spent any time thinking about it or designing it.

karayakar commented 2 years ago

@davidfowl Thank you David, I knew you did some POC on that before, thats why I asked actually I have done very similar solution to this one, will share soon.