AssemblyLoadContext AssemblyLoadContext AssemblyLoadContext AssemblyLoadContext Class

Definition

Represents the runtime's concept of a scope for binding. This class is abstract.

public ref class AssemblyLoadContext abstract
public abstract class AssemblyLoadContext
type AssemblyLoadContext = class
Public MustInherit Class AssemblyLoadContext
Inheritance
AssemblyLoadContextAssemblyLoadContextAssemblyLoadContextAssemblyLoadContext

Remarks

The AssemblyLoadContext represents a load context. Conceptually, a load context creates a scope for loading, resolving, and potentially unloading a set of assemblies.

AssemblyLoadContext is an abstract class. The AssemblyLoadContext.Load(AssemblyName) needs to be implemented to create a concrete class.

The AssemblyLoadContext exists primarily to provide assembly binding 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.

Note

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:

Application usage

An application can create its own AssemblyLoadContext to create a custom solution for advanced binding scenarios. The customization focuses on defining dependency resolution mechanisms.

The AssemblyLoadContext provides two extension points to implement managed assembly resolution:

  1. The AssemblyLoadContext.Load(AssemblyName) provides the first chance for the AssemblyLoadContext to resolve the assembly, load it and return it. If the AssemblyLoadContext.Load(AssemblyName) returns null, the loader will try to load the assembly into the AssemblyLoadContext.Default.
  2. 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 which is sufficient for most scenarios.

Technical challenges

  • It is not possible to load multiple versions of the runtime in a single process.

    Caution

    Loading multiple copies or different versions of framework assemblies can lead to unexpected and hard to diagnose behavior.

    Tip

    Use process boundaries with remoting and/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 which 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 binding isolation process creates multiple types with the same type name.

    • This can lead to confusing error messages, for example "type Foo.Foo cannot be cast to type Foo.Foo."
    • Marshaling across isolation boundaries is non-trivial. A typical solution would be to use an interface defined in an assembly which is only loaded into the default load context.

Constructors

AssemblyLoadContext() AssemblyLoadContext() AssemblyLoadContext() AssemblyLoadContext()

Initializes a new instance of the AssemblyLoadContext class.

AssemblyLoadContext(Boolean) AssemblyLoadContext(Boolean) AssemblyLoadContext(Boolean) AssemblyLoadContext(Boolean)

Initializes a new instance of the AssemblyLoadContext class with a value that indicates whether unloading is enabled.

AssemblyLoadContext(String, Boolean) AssemblyLoadContext(String, Boolean) AssemblyLoadContext(String, Boolean) AssemblyLoadContext(String, Boolean)

Initializes a new instance of the AssemblyLoadContext class with a name and a value that indicates whether unloading is enabled.

Properties

All All All All

Returns a collection of all AssemblyLoadContext instances.

Assemblies Assemblies Assemblies Assemblies

Returns a collection of the Assembly instances loaded in the AssemblyLoadContext.

CurrentContextualReflectionContext CurrentContextualReflectionContext CurrentContextualReflectionContext CurrentContextualReflectionContext

Gets the AssemblyLoadContext set by the most recent call to EnterContextualReflection().

Default Default Default Default

Gets the default AssemblyLoadContext. The default context contains the main application assembly and its static dependencies.

IsCollectible IsCollectible IsCollectible IsCollectible

Gets a value that indicates whether this AssemblyLoadContext is collectible.

Name Name Name Name

Get the name of the AssemblyLoadContext.

Methods

EnterContextualReflection() EnterContextualReflection() EnterContextualReflection() EnterContextualReflection()

Sets CurrentContextualReflectionContext to thisAssemblyLoadContext.

EnterContextualReflection(Assembly) EnterContextualReflection(Assembly) EnterContextualReflection(Assembly) EnterContextualReflection(Assembly)

Sets CurrentContextualReflectionContext to the AssemblyLoadContext which loaded the assembly.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()

Allows the object to try to free resources and perform other cleanup operations before it's reclaimed by garbage collection.

GetAssemblyName(String) GetAssemblyName(String) GetAssemblyName(String) GetAssemblyName(String)

Gets an AssemblyName for an assembly path.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLoadContext(Assembly) GetLoadContext(Assembly) GetLoadContext(Assembly) GetLoadContext(Assembly)

Gets the AssemblyLoadContext containing the specified Assembly.

GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
Load(AssemblyName) Load(AssemblyName) Load(AssemblyName) Load(AssemblyName)

When overridden in a derived class, allows an assembly to be resolved and loaded based on its AssemblyName.

LoadFromAssemblyName(AssemblyName) LoadFromAssemblyName(AssemblyName) LoadFromAssemblyName(AssemblyName) LoadFromAssemblyName(AssemblyName)

Resolves and loads an assembly given its AssemblyName.

LoadFromAssemblyPath(String) LoadFromAssemblyPath(String) LoadFromAssemblyPath(String) LoadFromAssemblyPath(String)

Loads the contents of an assembly file on the specified path.

LoadFromNativeImagePath(String, String) LoadFromNativeImagePath(String, String) LoadFromNativeImagePath(String, String) LoadFromNativeImagePath(String, String)

Loads the contents of the native image of a managed assembly file on the specified path.

LoadFromStream(Stream) LoadFromStream(Stream) LoadFromStream(Stream) LoadFromStream(Stream)

Loads the assembly with a common object file format (COFF)-based image containing a managed assembly.

LoadFromStream(Stream, Stream) LoadFromStream(Stream, Stream) LoadFromStream(Stream, Stream) LoadFromStream(Stream, Stream)

Loads the assembly with a common object file format (COFF)-based image containing a managed assembly, optionally including symbols for the assembly.

LoadUnmanagedDll(String) LoadUnmanagedDll(String) LoadUnmanagedDll(String) LoadUnmanagedDll(String)

Allows derived class to load an unmanaged library by name.

LoadUnmanagedDllFromPath(String) LoadUnmanagedDllFromPath(String) LoadUnmanagedDllFromPath(String) LoadUnmanagedDllFromPath(String)

Loads an unmanaged library from the specified path.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
SetProfileOptimizationRoot(String) SetProfileOptimizationRoot(String) SetProfileOptimizationRoot(String) SetProfileOptimizationRoot(String)

Sets the root path where the optimization profiles for this load context are stored.

StartProfileOptimization(String) StartProfileOptimization(String) StartProfileOptimization(String) StartProfileOptimization(String)

Starts the profile optimization for the specified profile.

ToString() ToString() ToString() ToString()

Returns the string representation of this load context.

Unload() Unload() Unload() Unload()

Initiates an unload of this AssemblyLoadContext.

Events

Resolving Resolving Resolving Resolving

Occurs when the resolution of an assembly fails when attempting to load into this assembly load context.

ResolvingUnmanagedDll ResolvingUnmanagedDll ResolvingUnmanagedDll ResolvingUnmanagedDll

Occurs when the resolution of a native library fails.

Unloading Unloading Unloading Unloading

Occurs when the AssemblyLoadContext is unloaded.

Applies to

See also