-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Description
After migrating to .NET 5 some C++/CLI assemblies are loading into new AssemblyLoadContext (IsolatedComponentLoadContext), which leads to loading dependencies one more time to this new AssemblyLoadContext.
Loading all assemblies manually into default AssemblyLoadContext using AssemblyLoadContext.Default.LoadFromAssemblyPath on startup do not solve the problem. The same C++/CLI assembly is loaded again (into IsolatedComponentLoadContext) on first code execution from that assembly despite being already loaded into default AssemblyLoadContext. I end up with many assemblies loaded multiple times.
What's interesting some assemblies are loading again into new AssemblyLoadContext only if first code execution happens on task ... But moving execution to main thread did not solve problem for every assembly.
All C++/CLI assemblies in this project are very similar. I don't see reason why some of them lands in new context.
Is there something I can do to force loading all assemblies into default AssemblyLoadContext?
Regression?
Everything works fine if compiled using .NET 4.6.2.
Other information
Framework: net5.0-windows.
Architecture: x86.
Program contains of C++/CLI and pure .NET assemblies. Startup assembly (exe) is pure .NET.
Why it is a problem
When some assemblies are loaded multiple times then it lead to lot of errors. Every instance of assembly has its own static values. Some part of code uses uninitialized statics, causes null exceptions. Type checking is not working when the same types are from different assemblies instances. etc.
Call Stack when new AssemblyLoadContext is created
System.Private.CoreLib.dll!System.Runtime.Loader.AssemblyLoadContext.LoadFromInMemoryModule(System.IntPtr moduleHandle) Line 93 C#
System.Private.CoreLib.dll!Internal.Runtime.InteropServices.InMemoryAssemblyLoader.LoadInMemoryAssembly(System.IntPtr moduleHandle, System.IntPtr assemblyPath) Line 36 C#
[Native to Managed Transition]
System.Private.CoreLib.dll![Frames below may be incorrect and/or missing, native debugger attempting to walk managed call stack] Unknown
Ijwhost.dll!start_runtime_and_get_target_address(unsigned int cookie) Line 150 C++
Ijwhost.dll!_start_runtime_thunk_stub@0() Line 36 Unknown
[Managed to Native Transition]
Amag.App.Unmanaged.dll!Amag::App::Unmanaged::MVendoAppLoader::LoadUserProfile(Amag::App::VendoAppStartConfig^ config) Line 117 C++
Amag.App.dll!Amag.App.VendoAppLoader.LoadUserProfile(bool isSwitch) Line 467 C#
...
Amag.App.dll!Amag.App.Program.Main(string[] args) Line 20 C#