Newtonsoft.Json
has a bug - yes! this actually happens on one of the most popular/used nuget packages- We want to patch the bug and make sure all our projects and their transitive packages use this version, instead
These answers were provided with the help of
Also refer to this StackOverflow question I asked and they answered.
1. Fork then clone Newtonsoft.Json
git clone ... etc
This is your custom code
This is a bit tricky. We need to export something called a "public key" to a file. We then will use this file, later on.
Easiest way to do this is to find an existing copy of the newtonsoft.json.nupkg file. For windows, this is:
C:\Users\<username>\.nuget\packages\newtonsoft.json\12.0.3\lib\netstandard2.0>sn -e Newtonsoft.Json.dll backup-newtonsoft.json-public-key.snk
Notice the:
username
in the path. Change to whatever yours is- the
version
in the path. I'm doing this against v12.0.3
.
Then copy the backup-newtonsoft.json-public-key.snk
to your fork + cloned repo and copy it to the <repo root>\src\Newtonsoft.Json\
folder.
First change the version in these places (using SEMVER):
<AssemblyVersion>12.1.0.0</AssemblyVersion>
<FileVersion>12.1.0</FileVersion>
<VersionPrefix>12.1.0</VersionPrefix>
<VersionSuffix>beta1</VersionSuffix>
Next add the delay signing stuff. This uses that file you exported in step #3, above:
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>backup-newtonsoft.json-public-key.snk</AssemblyOriginatorKeyFile>
<DelaySign>true</DelaySign>
I added this _right underneath the last item in this <PropertyGroup>
:
<EmbedUntrackedSources>true</EmbedUntrackedSources> <-- last line
<SignAssembly>true</SignAssembly> <-- start of new lines I've added
..... etc .....
From the repo root:
dotnet pack -c RELEASE -o .\custom-nuget-packages\ .\Newtonsoft.Json\Newtonsoft.Json.csproj
This will now create the NuGet packages in a new custom-nuget-packages
folder.
Now copy these newly generated nuget packages (.nupkg and .snupkg
) from this repo to your own other/custom repo.
e.g.
<other-repo-root>/custom-nuget-packages/Newtonsoft.Json.12.1.0-beta1.nupkg
To use this nuget package, we need to be able to 'find' them. The easiest way to is to add (or update if it already exists) a nuget.config
file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="localhost-custom-nugets" value="custom-nugets/" />
</packageSources>
</configuration>
This adds a new nuget location to any existing configuration settings.
And we're done. Add this to your .csproj
<PackageReference Include="Newtonsoft.Json" Version="12.1.0-beta1" />
I've included 2x sample demo's to demonstrate
- โ not working (the dll was not delayed signed)
- โ working
With the not working, you will notice that it doesn't compile because it's still after a specific version of Newtonsoft.Json
With the working one, it compiles.
Finally, I added a NEW method to my custom Newtonsoft.Json library to proove that my code is using my custom version:
// Proof -> my custom Newtonsoft.Json dll is being used.
Console.WriteLine(JsonConvert.HelloWorldTest());