C#C
C#2y ago
Pdawg

Forcefully unloading AssemblyLoadContext

I'm building a plugin system for my WinUI 3 C# app. Currently, the plugin system uses an AssemblyLoadContext for each plugin, but I want to be able to unload the plugins on demand. ALC.Unload places the assembly in line for GC collection, but it doesn't actually unload the code until all event handlers are disconnected, and all running threads suspend. While this is usually a good thing, I need to be able to control the plugins on demand. In an environment like this, someone could easily create a malicious block of code that continues to run in the background without the user ever noticing; or, poorly written code could continue to run even if it wasn't intended to do so. Each plugin has a Shutdown method, and that is always called before the unload is requested, so plugins can gracefully shutdown and save data or do whatever else they need to do to ensure that user data isn't lost.

So, is there a way that I can force an ALC to unload the plugin right there and then instead of waiting for it to finish up what it's doing?
image.png
Was this page helpful?