Answered by:
CimSession fails on an Initialization exception being thrown from MS.Mgmt.Infrastructure.Native in Core 2.0

Question
-
I'm kind of lost on the .net assemblies. I've been exploring different options of connecting to remote systems to execute WMI queries using dotnet Core 2.0 on a Win Server 2016 system x64. We have primarily been trying to use either the CimSession or ManagementScope (also a few others) but neither work, ending in exceptions.
When trying to create a CimSession, a TypeInitializationException is continually being thrown from Microsoft.Management.Infrastructure.native.OperationCallback: MissingMethodExcption - SystemAppDomain.DefineDynamicAssembly().
Does anyone have an idea why the MS.Mgmt.Infrastructure is causing this? I have the .Infrastructure.dll and .Infrastructure.Native.dll included with the project and have also cycled through various versions of Native dll. I've also read that there were a lot of changes under the hood for .net core 2, but I haven't been able to find anything concrete which explains why this is occurring.
Below are some of the code examples I've been following, albeit, there is no good details on framework and assembly usage:
- MSDN Library: aa389290(v=vs.85).aspx
- MS Blogs: Jose Barreto's sample-c-code-for-using-the-latest-wmi-classes-to-manage-windows-storage
Sorry for the bad links - I can't add them to posts yet. Thanks for any insight
Thursday, November 2, 2017 4:38 PM
Answers
-
There might be a solution using the dotnet corefx project on github. Work needs to go in for Linux support, but it would be really nice to get to the point where WQL can be run remotely from a Linux system. This would go through System.Management.ManagementScope vs MS.Mgmt.CimSession.
- Proposed as answer by Wendy ZangMicrosoft contingent staff Thursday, November 16, 2017 3:31 PM
- Marked as answer by Aelyx Friday, November 17, 2017 9:23 PM
Tuesday, November 14, 2017 3:59 PM
All replies
-
Hi Aelyx,
Thank you for posting here.
>>Does anyone have an idea why the MS.Mgmt.Infrastructure is causing this?
Could you provide the details of the exception? Could you provide some code for us to test?
And what do you want to do? Please provide more information, we will try our best to help you.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Monday, November 6, 2017 6:41 AM -
Monday, November 6, 2017 7:32 AMAnswerer
-
Hi, thanks for the response.
Here is the function, in it's most elementary form, which is throwing the exception. Below that is the exception information and the assembly info. Ultimately, We would like to be able to connect to remote systems using .Net Core 2.0 to execute WMI queries. Currently we're developing on a Windows x64 system, but we're hoping we'll be able to run this on any platform using Core 2 down the road.
private static int testCimSession() { int counter = 0; try { CimSession session = CimSession.Create("localhost"); IEnumerable<CimInstance> result = null; result = session.QueryInstances( @"root\cimv2", "WQL", "SELECT * FROM Win32_OperatingSystem"); foreach (CimInstance ci in result) { // <-- Throws exception here counter++; } } catch (Exception e) { counter = -1; Console.WriteLine("Error testing CimSession: {0}", e); } return counter; }
The Exception Being Thrown:
e { System.TypeInitializationException: The type initializer for 'Microsoft.Management.Infrastructure.Native.OperationCallbacks' threw an exception. ---> System.MissingMethodException: Method not found: 'System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'. at Microsoft.Management.Infrastructure.Native.OperationCallbacks..cctor() --- End of inner exception stack trace --- at Microsoft.Management.Infrastructure.Options.Internal.OperationOptionsExtensionMethods.GetOperationCallbacks(CimOperationOptions operationOptions) at Microsoft.Management.Infrastructure.Options.Internal.OperationOptionsExtensionMethods.GetOperationCallbacks(CimOperationOptions operationOptions, CimAsyncCallbacksReceiverBase acceptCallbacksReceiver) at Microsoft.Management.Infrastructure.CimSession.QueryInstancesCore(String namespaceName, String queryDialect, String queryExpression, CimOperationOptions options, CimAsyncCallbacksReceiverBase asyncCallbacksReceiver) at Microsoft.Management.Infrastructure.CimSession.<>c__DisplayClass61_0.<QueryInstances>b__0(CimAsyncCallbacksReceiverBase asyncCallbacksReceiver) at Microsoft.Management.Infrastructure.Internal.Operations.CimSyncEnumerableBase`2.GetEnumerator() at Zenoss.MicrosoftCore.ConnCimSession.testCimSession() in C:\Users\solutions.SOL-WIN\source\repos\RemoteConnTest\RemoteConnTest\ConnCimSession.cs:line 47 } System.Exception {System.TypeInitializationException}
Assembly Information:
Assembly: Microsoft.Management.Infrastructure Version: 10.0.14393.0 C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Management.Infrastructure\v4.0_1.0.0.0__31bf3856ad364e35\Microsoft.Management.Infrastructure.dll Assembly: Microsoft.Management.Infrastructure.Native Version: 10.0.14393.0 (rs1_release.160715-1616) C:\Windows\Microsoft.NET\assembly\GAC_64\Microsoft.Management.Infrastructure.Native\v4.0_1.0.0.0__31bf3856ad364e35\Microsoft.Management.Infrastructure.Native.dll
- Edited by Aelyx Thursday, November 9, 2017 1:02 AM
Tuesday, November 7, 2017 6:21 PM -
There might be a solution using the dotnet corefx project on github. Work needs to go in for Linux support, but it would be really nice to get to the point where WQL can be run remotely from a Linux system. This would go through System.Management.ManagementScope vs MS.Mgmt.CimSession.
- Proposed as answer by Wendy ZangMicrosoft contingent staff Thursday, November 16, 2017 3:31 PM
- Marked as answer by Aelyx Friday, November 17, 2017 9:23 PM
Tuesday, November 14, 2017 3:59 PM -
Hi Aelyx,
Thanks for your sharing.
Please mark the useful reply as answer. This will make answer searching in the forum and be beneficial to community members as well.
If you have something else about this issue, please feel free to contact us.
Best Regards,
WendyMSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Thursday, November 16, 2017 3:31 PM