Load Context Class
Represents the runtime's concept of a scope for assembly loading.
public ref class AssemblyLoadContext abstract
public abstract class AssemblyLoadContext
type AssemblyLoadContext = class
Public MustInherit Class AssemblyLoadContext
The AssemblyLoadContext represents a load context. Conceptually, a load context creates a scope for loading, resolving, and potentially unloading a set of assemblies.
The AssemblyLoadContext exists primarily to provide assembly loading isolation. It allows multiple versions of the same assembly to be loaded within a single process. It replaces the isolation mechanisms provided by multiple AppDomain instances in the .NET Framework.
AssemblyLoadContext does not provide any security features. All code has full permissions of the process.
Usage in the runtime
The runtime implements two assembly load contexts:
- AssemblyLoadContext.Default represents the runtime's default context, which is used for the application main assembly and its static dependencies.
- The Assembly.LoadFile(String) method isolates the assemblies it loads by instantiating the most basic AssemblyLoadContext. It has a simplistic isolation scheme that loads each assembly in its own AssemblyLoadContext with no dependency resolution.
An application can create its own AssemblyLoadContext to create a custom solution for advanced scenarios. The customization focuses on defining dependency resolution mechanisms.
The AssemblyLoadContext provides two extension points to implement managed assembly resolution:
- The AssemblyLoadContext.Load(AssemblyName) method provides the first chance for the AssemblyLoadContext to resolve, load, and return the assembly. If the AssemblyLoadContext.Load(AssemblyName) method returns
null, the loader tries to load the assembly into the AssemblyLoadContext.Default.
- If the AssemblyLoadContext.Default is unable to resolve the assembly, the original AssemblyLoadContext gets a second chance to resolve the assembly. The runtime raises the Resolving event.
Additionally, the AssemblyLoadContext.LoadUnmanagedDll(String) virtual method allows customization of the default unmanaged assembly resolution. The default implementation returns
null, which causes the runtime search to use its default search policy. The default search policy is sufficient for most scenarios.
It is not possible to load multiple versions of the runtime in a single process.
Loading multiple copies or different versions of framework assemblies can lead to unexpected and hard-to-diagnose behavior.
Use process boundaries with remoting or interprocess communication to solve this isolation problem.
The timing of assembly loading can make testing and debugging difficult. Assemblies are typically loaded without their dependencies immediately being resolved. The dependencies are loaded as they are needed:
- When code branches into a dependent assembly.
- When code loads resources.
- When code explicitly loads assemblies.
The implementation of AssemblyLoadContext.Load(AssemblyName) can add new dependencies that may need to be isolated to allow different versions to exist. The most natural implementation would place these dependencies in the default context. Careful design can isolate the new dependencies.
The same assembly is loaded multiple times into different contexts.
- This can lead to confusing error messages, for example "Unable to cast object of type 'Sample.Plugin' to type 'Sample.Plugin'".
- Marshaling across isolation boundaries is non-trivial. A typical solution is to use an interface defined in an assembly that's only loaded into the default load context.
Initializes a new instance of the AssemblyLoadContext class.
Initializes a new instance of the AssemblyLoadContext class with a value that indicates whether unloading is enabled.
Initializes a new instance of the AssemblyLoadContext class with a name and a value that indicates whether unloading is enabled.
Returns a collection of all AssemblyLoadContext instances.
Gets the default AssemblyLoadContext. The default context contains the main application assembly and its static dependencies.
Gets a value that indicates whether this AssemblyLoadContext is collectible.
Get the name of the AssemblyLoadContext.
Determines whether the specified object is equal to the current object.(Inherited from Object)
Allows the object to try to free resources and perform other cleanup operations before it's reclaimed by garbage collection.
Gets an AssemblyName for an assembly path.
Serves as the default hash function.(Inherited from Object)
Gets the Type of the current instance.(Inherited from Object)
When overridden in a derived class, allows an assembly to be resolved and loaded based on its AssemblyName.
Resolves and loads an assembly given its AssemblyName.
Loads the contents of an assembly file on the specified path.
Loads the contents of the native image of a managed assembly file on the specified path.
Loads the assembly with a common object file format (COFF)-based image containing a managed assembly.
Loads the assembly with a common object file format (COFF)-based image containing a managed assembly, optionally including symbols for the assembly.
Allows derived class to load an unmanaged library by name.
Loads an unmanaged library from the specified path.
Creates a shallow copy of the current Object.(Inherited from Object)
Sets the root path where the optimization profiles for this load context are stored.
Starts the profile optimization for the specified profile.
Returns the string representation of this load context.
Initiates an unload of this AssemblyLoadContext.
Occurs when the resolution of an assembly fails when attempting to load into this assembly load context.
Occurs when the resolution of a native library fails.
Occurs when the AssemblyLoadContext is unloaded.