C
C#2y ago
peppy

Plugin attempts to load dependency again, despite it already being loaded?

I have a .NET (.NET 7, for posterity) hosting from C++ situation. I followed the .NET hosting tutorial and the handoff to my C# DLL works fine. The C# DLL then attempts to load several plugins, and inspect their methods for various attributes. See attached images. fhclrhost is the C# DLL that is being hosted, and fhcshook is the plugin. Both of them have a dependency on fhcorlib, which is loaded lazily by fhclrhost, as it is the first to run. However, when fhcshook is loaded by means of Assembly.LoadFrom(...), it ignores the already loaded fhcorlib and tries to load it again. Why is that? I thought LoadFrom placed things in the same default load context as everything else? For posterity, fhcshook declares its dependency as in the plugin tutorial on MSDN:
<ItemGroup>
<ProjectReference Include="..\Fahrenheit.CoreLib\Fahrenheit.CoreLib.csproj">
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fahrenheit.CoreLib\Fahrenheit.CoreLib.csproj">
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>
</ItemGroup>
while fhclrhost simply declares its dependency as such:
<ItemGroup>
<ProjectReference Include="..\Fahrenheit.CoreLib\Fahrenheit.CoreLib.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fahrenheit.CoreLib\Fahrenheit.CoreLib.csproj" />
</ItemGroup>
2 Replies
peppy
peppyOP2y ago
There should be no version mismatch since these are part of the same solution, and are built at the same time Solved... it turns out that hosting code makes a very asinine decision.
The helper will always load the assembly into an isolated load context. This is the case regardless if the requested assembly is also available in the default load context or not.
The helper will always load the assembly into an isolated load context. This is the case regardless if the requested assembly is also available in the default load context or not.
So what happens is that fhcorlib was implicitly loaded into that isolated load context, but then Assembly.LoadFrom would load fhcshook into the default ALC Therefore, no fhcorlib from its perspective
peppy
peppyOP2y ago
GitHub
AssemblyLoadContext: requiring full cooperation to stay "inside" is...
Description My overall scenario is that there is a native app, and I want to use .NET 5 C# to write a plugin for that app (see also: #1633). I have tried porting to .NET Core before, but each time ...
Want results from more Discord servers?
Add your server