Skip to content

AssemblyLoadContext load "System.Management" throw System.PlatformNotSupportedException #110604

@tp1415926535

Description

@tp1415926535

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

  1. Using the Plug-in Example: https://github.com/dotnet/samples/tree/main/core/tutorials/Unloading.

  2. Add CopyLocalLockFileAssemblies and PackageReference in the "Plugin" project .csproj:

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="System.Management" Version="8.0.0" />
  </ItemGroup>
  1. And add code in the GetMessage() method of the PluginClass:
ManagementObjectSearcher searcher = new ManagementObjectSearcher();
  1. 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"));
  1. 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

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions