core
core copied to clipboard
The type initializer for 'System.Management.WmiNetUtilsHelper' threw an exception
I am attempting to move a Windows only .Net Core 3.1 application to .Net Core 6.0
In there I am using this statement:
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher($"SELECT CommandLine FROM Win32_Process WHERE ProcessId={current_process.Id}"))
Relevant Packages in use are:
<ItemGroup>
<PackageReference Include="System.Management" Version="6.0.0" />
</ItemGroup>
This gives an error
Unhandled exception. System.TypeInitializationException: The type initializer for 'System.Management.ManagementPath' threw an exception.
---> System.TypeInitializationException: The type initializer for 'System.Management.WmiNetUtilsHelper' threw an exception.
---> System.TypeLoadException
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointerInternal(IntPtr , Type )
at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer[TDelegate](IntPtr )
at System.Management.WmiNetUtilsHelper.LoadDelegate[TDelegate](TDelegate& , IntPtr , String )
at System.Management.WmiNetUtilsHelper..cctor()
--- End of inner exception stack trace ---
at System.Management.MTAHelper.IsNoContextMTA()
at System.Management.MTAHelper.CreateInMTA(Type )
at System.Management.ManagementPath.CreateWbemPath(String )
at System.Management.ManagementPath..ctor(String path)
at System.Management.ManagementPath..cctor()
--- End of inner exception stack trace ---
at System.Management.ManagementPath.get_DefaultPath()
at System.Management.ManagementScope._Clone(ManagementScope , IdentifierChangedEventHandler )
at System.Management.ManagementScope._Clone(ManagementScope )
at System.Management.ManagementObjectSearcher..ctor(ManagementScope , ObjectQuery , EnumerationOptions )
Hello, @genifycom. I was unable to reproduce this error using the information provided. Could you produce a minimal repro application that can generate this error please?
Here's the application I created in an attempt to recreate the error:
repro7-51.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Management" Version="6.0.0" />
</ItemGroup>
</Project>
Program.cs
using System.Diagnostics;
using System.Management;
var current_process = Process.GetCurrentProcess();
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(
$"SELECT CommandLine FROM Win32_Process WHERE ProcessId={current_process.Id}"))
{
foreach (var commandLine in searcher.Get())
{
Console.WriteLine(commandLine["CommandLine"]);
}
}
Output
PS C:\Users\jeff-\git\test\sysman> dotnet run
"C:\Users\jeff-\git\test\sysman\bin\Debug\net6.0-windows\repro7051.exe"
PS C:\Users\jeff-\git\test\sysman>
This issue has been automatically closed due to no response from the original author. Please feel free to reopen it if you have more information that can help us investigate the issue further.
Hi Jeff,
After further investigation, the issue is when you use:
<PublishTrimmed>true</PublishTrimmed>
If you add that to your test project csproj, it will trigger the error.
System.TypeInitializationException: 'The type initializer for 'System.Management.ManagementPath' threw an exception.' NotSupportedException: Built-in COM has been disabled via a feature switch. See https://aka.ms/dotnet-illink/com for more information.
Thanks
@genifycom Did you ever get a response from @jeffhandley or anyone else with regard to this error?
@genifycom I'm sorry I overlooked the responses here. The PublishTrimmed detail you added helps. As the error details explain, along with the documentation link included, you would need to take extra steps to ensure the COM features are enabled and working in the trimmed mode. Specifically, you would need to follow the guidance provided here: https://learn.microsoft.com/en-us/dotnet/standard/native-interop/tutorial-comwrappers
Did you overcome the blocker via that guidance, or did you remain stuck?
This issue is stale because there has been no response to a request for more information for 7 days.
This issue was closed because there was no response to a request for more information for 10 days.