C
C#•5mo ago
Fexty

Very specific question about AssemblyLoadContext and Assembly Resolution

So a bit of context: I'm developing a plugin system for modding a game (hopefully this is allowed here). It has a native dll which hosts the .NET 8 runtime using hostfxr. Then there are 2 assemblies A.dll and B.dll - A.dll is loaded into the default ALC by the native dll using hdt_load_assembly - B.dll is loaded into a unique custom ALC created by A.dll - B.dll then loads plugins into their own custom ALCs (one ALC per plugin) Inside all of those ALCs I'm using AssemblyDependencyResolver to resolve dependencies, or I delegate it to the "parent" ALC if it can't be resolved, which eventually ends up with the Default ALC. Now I tried creating a WPF assembly and I'm having two issues: 1. Since AssemblyDependencyResolver doesn't resolve framework assemblies as far as I understand it, I just try calling AssemblyLoadContext.Default.LoadFromAssemblyName when one of these is requested. However the default ALC fails to resolve these assemblies for some reason. (Throws FileNotFoundException) 2. I tried brute-forcing it by simply manually resolving the path to C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\8.0.1\ref\net8.0 (which is where the WPF assemblies seem to be installed for me) and calling AssemblyLoadContext.Default.LoadFromAssemblyPath directly. However doing this I get a BadImageFormatException. Here is the exact exception thrown:
System.BadImageFormatException: Could not load file or assembly 'PresentationFramework, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Reference assemblies cannot be loaded for execution. (0x80131058)
For reference: I have also attempted this with WinForms. It also had the first issue of not being able to resolve the dependencies, but for that the brute force approach worked. I am honestly stumped here and would really appreciate some help. I can provide source code as well if necessary. 🙂
3 Replies
Fexty
Fexty•5mo ago
Apologies for the wall of text, but I felt it necessary to give enough context
Mayor McCheese
Mayor McCheese•5mo ago
if the game publicly supports modding then discussion is allowed
Fexty
Fexty•4mo ago
Ah. That's a shame. It doesn't, else I wouldn't have such an elaborate setup 😅 In case anyone ever finds this (which is unlikely but whatever). All I had to do was add the framework references to the *.runtimeconfig.json that I pass to hostfxr