4

Related to this question: How to properly add .NET assemblies to Powershell session?

I used this code to likewise try to load a .NET Core 3.1 assembly into a PowerShell session.

I used this code:

# Load Assembly Add-Type -Path "I:\DW\tools\bin\Release\netcoreapp3.1\win-x64\publish\DataTools.dll" -PassThru if ($Error.Count -gt 0) { Write-Host $Error[0].Exception.LoaderExceptions } 

and I got this error:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Vers ion=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependenc ies. The system cannot find the file specified.

Then I built my assembly with this command to make it "self-contained":

dotnet publish -r win-x64 -c Release --self-contained 

However, now I am getting these LoaderExceptions:

System.TypeLoadException: Could not load type 'System.Object' from assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' because the parent does not exist.

Am I missing something in my dotnet build statement? or do I need to include an additional assembly?

NOTE: I also have dotnet 5 preview installed.

3
  • 3
    I think if I install PowerShell v7, then I can run dotnet core. That may solve my problem Commented Jun 2, 2020 at 19:10
  • 2
    It is; only .NET Standard 2.0 and earlier assemblies are .NET Framework compatible (what Windows PowerShell uses to run). In order to run any .NET Core or .NET Standard 2.1 or later assemblies you must run PowerShell Core built with a version of .NET Core that's greater or equal to the version used to build the assemblies you're using. TL;DR Run the latest version of PowerShell Core and you should be good. Commented Jul 18, 2020 at 5:18
  • Yes, upgrading from PowerShell 6 to PowerShell 7 solved this for me, too. Thank you so much! I don't think I would have figured this out from the error message. Commented Oct 14, 2021 at 11:49

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.