4

I have the following .csx script:

#r "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Management.dll"
using System.Management;
ManagementClass cls = new ManagementClass("\\\\.\\root\\default:StdRegProv");

When I run this using dotnet script I get this error:

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.ManagementPath..ctor(String path)
    at System.Management.ManagementClass..ctor(String path)
    at Submission#0.<<Initialize>>d__0.MoveNext() in <..path..>\script.csx:line 3
 --- End of stack trace from previous location ---
    at Dotnet.Script.Core.ScriptRunner.Execute[TReturn](String dllPath, IEnumerable`1 commandLineArgs) in <...path...>\Temp\tmpBFB\Dotnet.Script.Core\ScriptRunner.cs:line 110

If I compile the following code in visual studio, everything works fine:

using System.Management;
class Script {
    static void Main() {
        ManagementClass cls = new ManagementClass("\\\\.\\root\\default:StdRegProv");
    }
}

Why ? How can I get my .csx script to work ?

12
  • Please supply a stack trace for the exception. It is not possible for new to return a null reference by itself Commented Oct 24, 2021 at 21:59
  • Works on my machine™, after correcting the path to v4.0_4.0.0.0__b03f5f7f11d50a3a. How you got an assembly in the GAC with such a wonky PublicKeyToken is hard to guess. Not one to get too excited about loading, I'd say. Commented Oct 24, 2021 at 22:01
  • Misspelled my key intetionally, thought it won't matter, sorry. Added the stack trace. Commented Oct 24, 2021 at 22:22
  • 1
    @KenWhite Not really Commented Oct 24, 2021 at 22:30
  • 2
    @MarkusPeterson When your code is running in an executable, it will most probably run on an STAThread. Apparently if you execute as script, you'll have an MTAThread and the ManagementClass has a problem with it. Googling for the error there are some results with your problem, for example here Commented Oct 24, 2021 at 23:12

1 Answer 1

1

dotnet script runs off .NET Core and I don't think WMI works in .NET Core as it needs COM interop. See this.

Also, global runtime dll for .NET CORE is not in GAC but rather in a runtime package store.

Sign up to request clarification or add additional context in comments.

1 Comment

Made it work using nuget and the package Microsoft.Windows.Compatibility (which includes System.Management). Now I'm trying to reproduce it, but can't get it to work again (probably I'm missing some steps). I'll update this with a step-by-step guide when I solve it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.