Assembly Loading Extension Points
This demo uses the extensions points available for affecting managed assembly loading in .NET and shows when they are called by the runtime. The application (AssemblyLoading) loads:
- an assembly that is part of the
Microsoft.NETCore.Appframework and thus one of the known application assemblies - an assembly (
MyLibrary) that is not referenced by the application and thus requires handling in one of the available extension points to be successfully loaded
The following hooks into the managed assembly loading algorithm are included in this demo:
Build and Run
Install .NET 8.0 SDK or later
Build the solution with
dotnet buildRun the generated binary - for example,
AssemblyLoading/bin/Debug/net5.0/AssemblyLoadingThe program will print information about its usage syntax
AssemblyLoading <context> [<extension-point>]
Example:
AssemblyLoading defaultThe default
AssemblyLoadContextis used. The extension points are implemented, but none of them load theMyLibraryassembly. The expected output will show each extension point being called:=== Loading 'System.Xml' === Successfully loaded assembly: - Assembly: System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - Load context: "Default" System.Runtime.Loader.DefaultAssemblyLoadContext #0 === Loading 'MyLibrary' === AssemblyLoadContext.Resolving event handler - Name: MyLibrary, Culture=neutral, PublicKeyToken=null; - Load context: "Default" System.Runtime.Loader.DefaultAssemblyLoadContext #0 AppDomain.AssemblyResolve event handler - Name: MyLibrary, Culture=neutral, PublicKeyToken=null System.IO.FileNotFoundException: Could not load file or assembly 'MyLibrary, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. File name: 'MyLibrary, Culture=neutral, PublicKeyToken=null'
Example:
AssemblyLoading custom alc-resolvingA new custom
AssemblyLoadContextis used. The extension points are implemented and the handler for theResolvingevent on the customAssemblyLoadContextloads theMyLibraryassembly. The expected output will show each extension point being called:=== Loading 'System.Xml' === AssemblyLoadContext.Load - Name: System.Xml, Culture=neutral, PublicKeyToken=null - Load context: "CustomALC" AssemblyLoading.Program+CustomALC #0 Successfully loaded assembly: - Assembly: System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - Load context: "Default" System.Runtime.Loader.DefaultAssemblyLoadContext #1 === Loading 'MyLibrary' === AssemblyLoadContext.Load - Name: MyLibrary, Culture=neutral, PublicKeyToken=null - Load context: "CustomALC" AssemblyLoading.Program+CustomALC #0 AssemblyLoadContext.Resolving event handler - Name: MyLibrary, Culture=neutral, PublicKeyToken=null; - Load context: "Default" System.Runtime.Loader.DefaultAssemblyLoadContext #1 AssemblyLoadContext.Resolving event handler - Name: MyLibrary, Culture=neutral, PublicKeyToken=null; - Load context: "CustomALC" AssemblyLoading.Program+CustomALC #0 Successfully loaded assembly: - Assembly: MyLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null - Load context: "CustomALC" AssemblyLoading.Program+CustomALC #0