Search code examples
jsonjson.netnuget

How to create a custom Newtonsoft.Json nuget to use in my .NET Core 3.1 app?


Issue:

How can I use a custom build of Newtonsoft.Json in my project which forces the transitive nuget packages to use my custom package?

Details:

We are using Newtonsoft.Json and have stumbled across a bug in it. There's an open PR to fix it, but it's going no where.

Instead, I've forked, patched and created my own nuget package - works fine!

Now, I've got a 3rd party nuget package which has an older version of Newtonsoft.Json in it .. and now it's complaining that I need to use that specific version. When I manually use the most recent version explicitly in my project, there's no errors from the transitive versions. Hmm.. okay. When I use my version, it complains saying it wants to transitive version added.

enter image description here

My guess is that my custom build of Newtonsoft.Json has publicKeyToken=null where as the official ones are publicKeyToken=30ad4fe6b2a6aeed

I also don't believe I can sign the custom dll to have the same publicKeyToken? (Side note: There is a Strong Name Key file in the repo, though. I'm not sure if i can use this?)

So is there anything I can do?

Is there a way to say to all transitive nuget packages to use my version of Newtonsoft.Json instead of the older, official one?

Update


Solution

  • In the repo there are two reasons because it doesn't build:

    1. Assmebly version 11.0.0.0 but dependencies require 12.0.0.0.

    You can fix that by increasing the version number of the dll.

    1. Mismatching signature.

    You can fix this by test- / delay-signing the .dll with the public key that Newtsoft.Json.dll uses or has in the repo (didn't check if the key in that repo is the one used for signing).

    You can accomplish both with the included dll in that repo by:

    (Done in a VS command prompt to have all the tools in place)

    1. Extract the public key from a public version of Newtonsoft.Json with sn -e Newtonsoft.Json.dll nsjson.snk (suggest C:\Users\YOURUSER\.nuget\packages\newtonsoft.json\12.0.3\lib\netstandard2.0 if NuGet downloaded that previously)

    2. IL-disassemble the dll using ildasm.exe /all Newtonsoft.Json.dll /outfile=Newtonsoft.Json.il

    3. Change the .ver 11:0:0:0 specification in the .il file to .ver 12:0:0:0

    4. Copy over nsjson.snk. Then reassemble and delay sign the assembly with ilasm.exe /dll /key=nsjson.snk Newtonsoft.Json.il

    For this to work I used the ilasm/ildasm versions from the microsoft.netcore.ilasm and microsoft.netcore.ildasm 5.0.0 NuGet packages (e.g. C:\Users\YOURUSER\.nuget\packages\microsoft.netcore.ilasm\5.0.0\runtimes\native\ilasm.exe if you installed these packages into any project)