Skip to content

System.Management not initializing on some Server SKUs #24536

Description

@danmoseley

Copied from dotnet/corefx#24719

@jianxuanbing commented on Nov 16
Hello,I have a questin.I use examples

ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
diskClass.Get();
Output.WriteLine("Logical Disk class has " + diskClass.Properties.Count + " properties");
but is not ok,throw exception

System.TypeInitializationException : The type initializer for 'System.Management.WmiNetUtilsHelper' threw an exception.
---- System.NullReferenceException : Object reference not set to an instance of an object.
   at System.Management.MTAHelper.IsNoContextMTA()
   at System.Management.MTAHelper.CreateInMTA(Type type)
   at System.Management.ManagementPath.CreateWbemPath(String path)
   at System.Management.ManagementClass..ctor(String path)

@pjanotti
Member
pjanotti commented on Nov 16
Hi, thanks for reporting it. I can't repro the issue. Please, provide the details and a small repro - feel free to open a separate issue with those details.

Notice that System.Management namespace is intended for legacy code only.

@jianxuanbing

jianxuanbing commented on Nov 16
Hi,I changed the compter,the result is normal.
I will try again.

@jianxuanbing

jianxuanbing commented on Nov 19
I use the same code. But at home when the computer tests is throw Exception.
But at company when the computer tests is ok.

        public static string GetName()
        {
            using (ManagementClass mc = new ManagementClass(WmiPathConst.Processor))
            {
                ManagementObjectCollection moc = mc.GetInstances();
                return moc.OfType<ManagementObject>()
                    .Select(mo => mo[ManagementObjectConst.Name].ToString())
                    .FirstOrDefault();
            }
        }

@STRATZ-Ken

STRATZ-Ken commented 5 days ago • edited
Same issue as @jianxuanbing except I am trying for the NetworkAdapaters.

I am running it on Windows 2012 R2 in a Hyper-V VM in 64 Bit. If any of this helps.

@wspait

wspait commented a day ago
We are also having the same issue as @jianxuanbing. We are using version 4.5.0-preview1-25914-04. This code works fine locally (Windows 10), but not when run from a clean Server 2012 R2 or Server 2016 Windows image.

static void Main(string[] args)
 {
        try
        {
            Console.WriteLine("Calling System.Management code");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\CIMV2", "SELECT * FROM Win32_SystemEnclosure");
            Console.WriteLine("Application complete");
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex);
        }     
}

This is the output:

Calling System.Management code
System.TypeInitializationException: The type initializer for 'System.Management.
WmiNetUtilsHelper' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Management.WmiNetUtilsHelper..cctor()
   --- End of inner exception stack trace ---
   at System.Management.MTAHelper.IsNoContextMTA()
   at System.Management.MTAHelper.CreateInMTA(Type type)
   at System.Management.ManagementPath.CreateWbemPath(String path)
   at System.Management.ManagementObjectSearcher..ctor(String scope, String queryString)
   at WmiNetUtilsHelperTest.Program.Main(String[] args)

@danmosemsft
Member
danmosemsft commented 20 hours ago
@wspait the S.Management package requires a native binary that is included in the full.NET Framework 4.x. The error message could be clearer (please open a new issue). Meanwhile please add 4.x to your images.

@STRATZ-Ken

STRATZ-Ken commented 10 hours ago
@danmosemsft All of my machines have .Net 4.6.1 installed as that is what I use to run my program on. So it should have that dependency. I cant seem to make the error bigger either, myself.

@danmosemsft
Member
danmosemsft commented 3 hours ago
Looks like if .NET 4.x is not found it would have bailed out cleanly. It gets further. It could be here:

                procAddr = Interop.Kernel32.GetProcAddress(loadLibrary, "Initialize");
                if( procAddr != IntPtr.Zero)
                {
                    Initialize_f  =(Initialize) Marshal.GetDelegateForFunctionPointer(procAddr, typeof(Initialize));
                }

        Initialize_f(CompatSwitches.AllowIManagementObjectQI);

The Initialize_f call is accidentally outside of the braces.

Although I would expect problems after this point anyway -- if the LL/GPA is failing.

@danmosemsft
Member
danmosemsft commented a minute ago
I set up a Server 2012 R2 machine (which has 4.7:2117.0 which I think is 4.7.1) and your code works fine.

@STRATZ-Ken @wspait are either of you willing to debug? Symbols and sources should be available. The code to put a breakpoint on is the static constructor of WmiNetUtilsHelper.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions