-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Type
Projects
Status