Skip to content

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

Closed

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

Activity

  1. ghost added
    area-AssemblyLoader-coreclr
    on Dec 11, 2024
  2. added
    untriaged
    on Dec 11, 2024
  3. huoyaoyuan commented on Dec 11, 2024

    @huoyaoyuan
    Member

    You are loading the wrong assembly file. bin\\Debug\\net7.0\\System.Management.dll is the fallback dll for unsupported platform. The implementation for Windows is at bin\\Debug\\net7.0\\runtimes\\win\\lib. You can inspect the loaded file from Module window in Visual Studio.

  4. tp1415926535 commented on Dec 11, 2024

    @tp1415926535
    Author

    You are loading the wrong assembly file. bin\\Debug\\net7.0\\System.Management.dll is the fallback dll for unsupported platform. The implementation for Windows is at bin\\Debug\\net7.0\\runtimes\\win\\lib. You can inspect the loaded file from Module window in Visual Studio.

    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.

  5. removed
    untriaged
    on Dec 11, 2024
  6. locked and limited conversation to collaborators on Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-AssemblyLoader-coreclr

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions