Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Newtonsoft.Json from Packages does not pass Strong Name Validation

Discussion in 'Package Manager' started by Deleted User, Jul 23, 2021.

  1. Deleted User

    Deleted User

    Guest

    Hello,

    Since updating unity to recent versions from 2021 branch we are forced to use Newtonsoft.Json from Packages. I have used this library for the long time and have pipeline build around it. Namely I generate classes based on TransformText and .json files. Unfortunately I cannot make full transition to the new version of Unity because I cannot use provided Newtonsoft.Json.dll from PackageCache instead off version I had before as it is not properly signed and does not pass *Strong name validation failed. (Exception from HRESULT: 0x8013141A)*.

    I'm not able to disable this broken Package using PackageManager, nor can I remove/update it manually.

    How I can force Unity to use libraries that actually work properly? Or using older Unity version is the only way?
     
  2. maximeb_unity

    maximeb_unity

    Unity Technologies

    Joined:
    Mar 20, 2018
    Posts:
    481
    Hi @Uteki,

    I'm sorry that you are running into this problem. I can think of two fairly simple workarounds:

    1) You can try embedding the "com.unity.nuget.newtonsoft-json" package in your project and replace the DLL in it with your own strong-named version. If it supports the same feature set needed for the Unity packages depending on it, it should work.
    2) You can create a simple Git repository that contains the contents of "com.unity.nuget.newtonsoft-json" package but with your DLL in it, and add a Git-based dependency to it in your project. That would make the Package Manager use that version instead of fetching it from the registry.

    The first workaround works well if you have a single project (or very few of them) to handle similarly. If you maintain multiple projects or frequently create new projects, the second workaround seems like a bit more automated.

    That said, I am curious as to how this used to work and stopped working with 2021. Were you using a custom version of "com.unity.nuget.newtonsoft-json"? Was that package just not in your project before, and now it happens to be present in your project because it's a new transitive dependency?
     
  3. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    653
    Hi @maximeb_unity
    We're having a similar problem with the same error:

    Code (CSharp):
    1. System.IO.FileLoadException : Could not load file or assembly 'Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)
    2. System.Security.SecurityException : Strong name validation failed. (Exception from HRESULT: 0x8013141A)
    We see this error when trying to run our Unit tests *outside* Unity.

    Inside Unity, all compiles and tests all pass.
    Outside Unity, code compiles (albeit with suspicious warning) and tests that use YamlDotNet fail with the error above (yes, irony that deserializing YAML is causing a JSON error!).

    This used to work. It changed when we added Unity Localization package. Before we had JsonDotNet package from the Asset Store in the project. Localization depends upon the Unity-provided com.unity.nuget.newtonsoft-json package so we had to remove the Asset Store version to remove a clash.

    Just to be clear, our tests are written to avoid any ECall errors etc. They all worked before introducing the Unity L10N and Unity JSON packages.

    Re. the "suspicious warnings", see attached txt which seems to suggest Unity's somehow forcing-in(?) System.Net.Http 4.0.0.0 but most things would prefer 4.2.0.0 ?? Not sure (1) if related or (2) how to affect though.

    Regarding the workarounds you suggested above, would they work in this situation? (specifically with our project requiring a Unity Package which in-turn requires the problem package?)

    Thanks in advance :)
     

    Attached Files:

  4. OutplayAndrew

    OutplayAndrew

    Joined:
    Aug 24, 2016
    Posts:
    1
    Hi Arkade.

    The team I am in had a similar issue as you, and we have a solution that worked well for us.

    The first, and simplest to describe, solution is using a non-Windows machine. We do this for our automated test-and-build pipeline on our linux machines.

    This obviously doesn't help when using windows machines. For our work around here we used Microsoft's Strong Naming tool (sn.exe) using the -Vr options targeting the Newtonsoft.json.dll bundled in the com.unity.nuget.newtonsoft-json.

    command:

    [path to sn tool directory]\sn.exe -Vr [path to dll directory]\Newtonsoft.json.dll

    We had to use the x64 version of the sn.exe, otherwise it won't work.
    We noticed that there is a sn.exe in a path bundled in unity, but that is not the same tool. The one to use is from Microsoft Windows SDK (https://developer.microsoft.com/en-us/windows/downloads/sdk-archive/)

    Note - this doesn't modify the DLL in any way. It only updates windows registries in order to be looser with the strictness when validating.

    I hope this helps!
     
  5. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    653
    Hey, @OutplayAndrew
    Thanks for answering!

    I tried `sn -Vr` on all the Newtonsoft.json.dll files I could find and... well, at first it failed then I remembered you said to use the x64 version and ... IT WORKED! THANK YOU!

    For future maddened minds, I originally used:

    Code (CSharp):
    1. "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.1 Tools\sn" -Vr
    On all the versions under the repository I could find:

    Code (CSharp):
    1. ./Library/PackageCache/com.unity.burst@1.6.6/.Runtime/Newtonsoft.Json.dll
    2. ./Library/PackageCache/com.unity.nuget.newtonsoft-json@3.0.2/Runtime/AOT/Newtonsoft.Json
    3. ./Library/PackageCache/com.unity.nuget.newtonsoft-json@3.0.2/Runtime/Newtonsoft.Json.dll
    The response each time was the same:

    Code (CSharp):
    1. Verification entry added for assembly 'Newtonsoft.Json,30AD4FE6B2A6AEED'
    Then I remembered the thing about x64 and checked. There's a sub-directory!

    Code (CSharp):
    1. "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.1 Tools\x64\sn" -Vr
    That was it! Pheeeeeyeew! I can run unit tests from command-line (and especially in Rider) again!
    Thank you thank you thank you!