Repository navigation
Description
Description
If System.Management is used in the plugin, loaded by AssemblyLoadContext and call method will throw error System.PlatformNotSupportedException:"System.Management currently is only supported for Windows desktop applications."
Reproduction Steps
-
Using the Plug-in Example: https://github.com/dotnet/samples/tree/main/core/tutorials/Unloading.
-
Add
CopyLocalLockFileAssembliesandPackageReferencein the "Plugin" project.csproj:
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Management" Version="8.0.0" />
</ItemGroup>- And add code in the
GetMessage()method of thePluginClass:
ManagementObjectSearcher searcher = new ManagementObjectSearcher();- Add code in "Host" project after
new HostAssemblyLoadContext:
var alc = new HostAssemblyLoadContext(assemblyPath);
alcWeakRef = new WeakReference(alc);
//load System.Management here:
alc.LoadFromAssemblyPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"..\\..\\..\\..\\Plugin\\bin\\Debug\\net7.0\\System.Management.dll"));- rebuild "Plugin" project and run "Host" console.
Expected behavior
Normal code execution.
Actual behavior
Error when the code executes to ManagementObjectSearcher searcher = new ManagementObjectSearcher();:
System.PlatformNotSupportedException:"System.Management currently is only supported for Windows desktop applications."Also, I found that once skip "step 4", it seems to execute properly.
But in practice I don't know which file in the folder will be the main dll, and I using System.Composition with ExportAttribute, so the path I pass to the AssemblyLoadContext constructor is the folder, like this:
//var alc = new HostAssemblyLoadContext(assemblyPath);
var alc = new HostAssemblyLoadContext(Path.GetDirectoryName(assemblyPath));So it has to load more than one dll by alc.LoadFromAssemblyPath(), otherwise it will throw error:
System.IO.FileNotFoundException:"Could not load file or assembly 'System.Management, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'"Overall System.Management doesn't look very compatible with context loading.
Regression?
No response
Known Workarounds
No response
Configuration
.NET 6/7/8 both like this.
Other information
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
huoyaoyuan commentedon Dec 11, 2024
You are loading the wrong assembly file.
bin\\Debug\\net7.0\\System.Management.dllis the fallback dll for unsupported platform. The implementation for Windows is atbin\\Debug\\net7.0\\runtimes\\win\\lib. You can inspect the loaded file from Module window in Visual Studio.tp1415926535 commentedon Dec 11, 2024
You're right, it works after I changed the target dll. In the past the it was loaded automatically without using AssemblyLoadContext, so I didn't pay attention to these... rules.