JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.73k stars 3.25k forks source link

Add-Type 'Newtonsoft.Json.dll' fails for 13.0.1 version and PowerShell 7 or 7.1 #2701

Open VasylZvarydchuk opened 2 years ago

VasylZvarydchuk commented 2 years ago

Hello, I updated Newtonsoft.Json library from 12.0.3 to 13.0.1 version and the following PowerShell code stopped working

Add-Type -Path '..\Newtonsoft.Json.dll' 

failing with error

Add-Type: Could not load file or assembly 'Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.

The same error is happening if to include Newtonsoft.Json into RequiredAssemblies PowerShell module definition file or if PowerShell loads some other assembly which has a reference to Newtonsoft.Json version 13.0.1.

My PowerShell version is

Name                           Value
----                           -----
PSVersion                      7.1.6
PSEdition                      Core
GitCommitId                    7.1.6
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

The same is happening for PowerShell 7.0. Fusion logs don't show anything unfortunately. Interesting that this code works fine

[System.Reflection.Assembly]::LoadFile("..\Newtonsoft.Json.dll")
mjlindsay commented 2 years ago

Just out of curiosity, if you right-click the Newtonsoft.Json.dll file, choose 'Properties' and check the 'Unblock' box, will PowerShell let you use AddType? Note, you may need to restart your PowerShell instance.

I was able to re-produce and fix this issue by unblocking the dll; depending on your download method, Windows may have it blocked by default. Of course, this doesn't affect the LoadFile method.

VasylZvarydchuk commented 2 years ago

Just out of curiosity, if you right-click the Newtonsoft.Json.dll file, choose 'Properties' and check the 'Unblock' box, will PowerShell let you use AddType? Note, you may need to restart your PowerShell instance.

I was able to re-produce and fix this issue by unblocking the dll; depending on your download method, Windows may have it blocked by default. Of course, this doesn't affect the LoadFile method.

Hi Michael, first in what tab is Unblock box located? I don't see it in my Windows 10. I guess it's applicable only if to download file from Internet. Next even if this option will work it can be used only locally and not on any machine like in Azure DevOps VM. Also, the question why the previous version worked and new one doesn't work on many machines including Azure DevOps VM and so on.

worthy7 commented 2 years ago

After installing the 13.0.0.1 nuget, my project is referencing version 13.0.0.0

image

When my app tries to run and references the dll somewhere, it crashes saying it was looking for version 13.0.0.0 but couldn't find it. image

My build output DLL is 13.0.0.1 image

worthy7 commented 2 years ago

I lied, it seems the project consuming this project was the one that did not have Newtonsoft.JSON installed, so it just used some one that it found from other assemblies.