I’ve run C# in PowerShell before, but I’ve always been lucky that the needed assemblies (and the appropriate versions of those assemblies) were already available in the Global Assembly Cache (GAC) on the machine I happen to be running the PS/C# script on.
(FYI, GAC Location = C:WindowsMicrosoft.NETassemblyGAC_MSIL)
So, now, I’m trying to use THE most popular NuGet package Json.Net aka Newtonsoft.Json:
https://www.nuget.org/packages/Newtonsoft.Json/10.0.3
It depends on newer versions of certain assemblies that are already present in my GAC. So, I figured I’d download those newer assemblies and install them to my GAC, and reference the newer versions when using the “Add-Type” cmdlet. Easy peasy, right?
NOPE.
Here’s a github gist of what I’m attempting:
https://gist.github.com/pldmgg/e487a5f3f1d0cc3963c2aa61705b7e04
(also, the Unzip-File function used within the above gist can be found here: https://github.com/pldmgg/misc-powershell/blob/master/MyFunctions/Unzip-File.ps1)
So the first thing that throws me for a loop is that when I get the FullNames of these newly downloaded assemblies, the reported versions don’t necessarily make sense. After you run my gist, $Assem gives me the following output (abbreviated to focus on the newly downloaded assemblies):
PS C:Userstestadmin> $Assem System.Runtime.Serialization.Primitives, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed ... System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Dynamic.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.ObjectModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ...
The only assembly that matches the version I downloaded is System.Runtime.Serialization.Primitives.
When I look in “C:WindowsMicrosoft.NETassemblyGAC_MSIL” in the respective assembly folders, subdirectories include the old version of the assembly and the new version of the assembly that I downloaded and installed to the GAC. If $Assem consistently referred to the older version in the GAC, that would be one thing, but it doesn’t necessarily do that.
In case you want to see the error messages for when I attempt to run…
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharpVersion3
…here they are:
https://gist.github.com/pldmgg/1499fa9aaef19d004e5f449dbe9f9bac
My other idea to get my $Source code to run is to make a Class Library in Visual Studio, and then use…
Add-Type -Path "$HOMEMyClassLibrary.dll"
…but that complains about dependencies as well…I guess when you compile a Class Library in Visual Studio, it doesn’t bundle the dependencies with it…which is…very frustrating…
Any advice/help would be greatly appreciated! Also, My C# code is probably abysmal, so if anyone wants to rip that apart, I’m all ears!
submitted by /u/fourierswager
[link] [comments]
The post Loading/Running C# in PowerShell – Dependecy Hell appeared first on How to Code .NET.