.NET › Assemblies & NuGet

What is assembly loading context, and how does AssemblyLoadContext enable plugin isolation in modern .NET?

Advanced

Answer

AssemblyLoadContext (ALC) replaces .NET Framework's AppDomain for isolating loaded assemblies -- you can create a custom, collectible ALC to load a plugin's assemblies in isolation from the main application and other plugins, with the ability to unload it later (freeing memory) once the plugin is no longer needed, something AppDomains could also do but with much heavier overhead and cross-domain marshaling complexity.
var alc = new AssemblyLoadContext("PluginContext", isCollectible: true);
var assembly = alc.LoadFromAssemblyPath("/plugins/MyPlugin.dll");
var pluginType = assembly.GetType("MyPlugin.Plugin");

// Later, unload the entire plugin and its assemblies:
alc.Unload();
Real-world example A plugin-based reporting application loads each third-party report generator DLL into its own collectible AssemblyLoadContext, letting administrators update or remove a plugin at runtime without restarting the whole application.

Common follow-ups: Why is default assembly unloading not possible without a custom collectible ALC?;What are common pitfalls with type identity across different ALCs?