title: Calling C# Method Within Powershell Core
body: <p>I'm attempting to call a C# dll within Powershell Core.<br>
  The C# dll has been created using <a href="https://docs.microsoft.com/en-us/dotnet/standard/net-standard" rel="nofollow noreferrer">.Net Standard</a>.<br><br>Here is the C# code I'm trying to call.</p>
  
  <pre><code>public partial class Fiery
  {
      private Fiery(){}
  
      /// &lt;summary&gt;
      /// Establishes authorized user access to the Fiery API features
      /// &lt;/summary&gt;
      /// &lt;param name="printer"&gt;&lt;/param&gt;
      /// &lt;param name="username"&gt;&lt;/param&gt;
      /// &lt;param name="password"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static EFI_Fiery_API.FieryLogin.Login Login(Printer printer, string username, string password)
      {
          // Apply Certificate.
          Console.WriteLine("{0}", IsCertificateValidationApplied.Value ? "Certificate Applied" : "Certificate Was Unable To Be Applied");
          EFI_Fiery_API.FieryLogin.Login loginResponse = SendLoginRequest(printer, username, password).Result;
  
          return loginResponse;
      }
  }
  </code></pre>
  
  <p><br>
  And here is the powershell script.</p>
  
  <pre><code>[System.Reflection.Assembly]::LoadFile('\\foo\bar\netstandard2.0\EFI-Fiery-API.dll')
  [EFI.Fiery]::AccessKey = "foo"
  
  $printer = New-Object EFI.Fiery+Printer -ArgumentList "0.0.0.0";
  $username = "admin";
  $password = [System.String]::Empty;
  
  $loginResult = New-Object EFI_Fiery_API.FieryLogin+Login -ArgumentList $printer,$username,$password;
  
  if ($loginResult.IsSuccess){
      #do something.
  }
  </code></pre>
  
  <p>This is the error</p>
  
  <blockquote>
    <p>New-Object : Cannot find an overload for "Login" and the argument count: "3".
    + ... ginResult = New-Object EFI_Fiery_API.FieryLogin+Login -ArgumentList $ ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand</p>
  </blockquote>
  
  <p>So I tried,</p>
  
  <pre><code>if ([EFI_Fiery_API.FieryLogin+Login]::new($printer,$username,$password)){
      #do something.
  }
  </code></pre>
  
  <p>And I received</p>
  
  <blockquote>
    <p>Cannot find an overload for "new" and the argument count: "3".
    + if ([EFI_Fiery_API.FieryLogin+Login]::new($printer,$username,$passwor ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest</p>
  </blockquote>
  
  <p>I finally tried</p>
  
  <pre><code>if ([EFI_Fiery_API.FieryLogin+Login]::new([EFI.Fiery+Printer]$printer,[System.String]$username, 
  [System.String]$password)){
      #do something.
  }
  </code></pre>
  
  <p>The error is the same as above.<br><br>
  Something that I thought was a little odd was the fact that I can invoke methods that have a single parameter.</p>
  
  <pre><code>if ([EFI.Fiery]::Logout($printer)){
      #works.
  }
  </code></pre>
  
  <p>Here is how the class is being classed in a C# application.</p>
  
  <pre><code>        // AccessKey.
          EFI.Fiery.AccessKey = "foo";
  
          // The Printer.
          EFI.Fiery.Printer printer = new EFI.Fiery.Printer("0.0.0.0");
  
          // Login To The Printer.
          var loginResults = EFI.Fiery.Login(printer, "admin", null);
  
          // Login To The Printer.
          if (loginResults.IsSuccess)
          {
  </code></pre>
  
  <p>Here Is The Error From The Exception.</p>
  
  <blockquote>
    <p>System.AggregateException: One or more errors occurred. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.\r\n   at EFI.Fiery.&lt;>c__DisplayClass41_0.&lt;b__0>d.MoveNext()\r\n   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine&amp; stateMachine)\r\n   at EFI.Fiery.&lt;>c__DisplayClass41_0.b__0()\r\n   at System.Threading.Tasks.Task<code>1.InnerInvoke()\r\n   at System.Threading.Tasks.Task.Execute()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n   at EFI.Fiery.&lt;SendLoginRequest&gt;d__41.MoveNext()\r\n   --- End of inner exception stack trace ---\r\n   at System.Threading.Tasks.Task</code>1.GetResultCore(Boolean waitCompletionNotification)\r\n   at EFI.Fiery.Login(Printer printer, String username, String password)\r\n   at CallSite.Target(Closure , CallSite , Type , Object , Object , Object )\r\n---> (Inner Exception #0) System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.\r\nFile name: 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'\r\n   at EFI.Fiery.&lt;>c__DisplayClass41_0.&lt;b__0>d.MoveNext()\r\n   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine&amp; stateMachine)\r\n   at EFI.Fiery.&lt;>c__DisplayClass41_0.b__0()\r\n   at System.Threading.Tasks.Task`1.InnerInvoke()\r\n   at System.Threading.Tasks.Task.Execute()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n   at EFI.Fiery.d__41.MoveNext()\r\n\r\nWRN: Assembly binding logging is turned OFF.\r\nTo enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.\r\nNote: There is some performance penalty associated with assembly bind failure logging.\r\nTo turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].\r\n&lt;---\r\n</p>
  </blockquote>
  
  <p>This tells me that I am missing Json package, I added it however, it didnt help, I will check that I am using the correct version.</p>
  
  <p>Ok Turns out I didnt have the assembly imported correctly,
  I ened up using <code>[System.Reflection.Assembly]::LoadFile(path to .net standard json dll)</code></p>
  
  <p>and it worked, Thanks @mklement0 for pointing out<code>$Error[0].Exception.InnerException</code> to get the innter exception in the hierarchy.</p>