Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Why do I get System.NullReferenceException in my c# script while my executable of the same code is working?

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 ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!