Mismatching versions of System.Text.Json

uds 25 Reputation points
2025-11-12T15:41:22.73+00:00

In our company, we develop 2 .NET solutions.

Solution 1 contains - amongst others - a couple of .NET Framework 4.7.2 class libraries used in other projects. Some of these class libraries reference the NuGet package System.Text.Json 9.0.11 because this is not natively part of .NET Frakework 4.7.2.

Solution 2 is a ASPNetCore web application targetting .NET 9 . This solution makes use of the class libraries provided by the other solution. Originally, this project also had a version of System.Text.Json installed because it had been upgraded from a .NetCore 3.1 solution using the .NET Upgrade Assistant. However, I recently removed this NuGet package. As far as I'm aware, System.Text.Json is part of the .NET 9 installation anyway, so installing it separately shouldn't be necessary.

Now, for several years we've been having a recurring issue: The web application builds fine, but when the code reaches a specific part, it crashes with the following error message:

System.IO.FileNotFoundException: "Could not load file or assembly 'System.Text.Json, Version=9.0.0.11, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Das System kann die angegebene Datei nicht finden."

Usually, we were always able to (temporarily) solve this issue by trying around with different combinations of versions of the package installed in both solutions. Then it went well for a while until, after installing some NuGet update, the trouble started anew. This is a real hassle for the one having to deal with it, plus it keeps binding resources every time the issue arises. Needless to say: We would very much like this solved once and for all!

What we are doing:

In Solution 1, we compile the needed class libraries in Release Mode. Then we copy the resulting dlls, the Microsoft dlls referenced by these libraries (Microsoft.Bcl.AsyncInterfaces.dll, System.Buffers.dll, and so on) and the created .dll.config files into the Assemblies directory of Solution 2. Solution 2 references these as external dependencies, Local copy is set to True.

What doesn't work:

As mentioned, compiling Solution 2 works just fine. The issue only occurs at run time. However, I noticed warnings at compile time of class MSB3277 that seem to be related to the problem:

Es wurden Konflikte zwischen verschiedenen Versionen von "System.Text.Json" gefunden, die nicht aufgelöst werden konnten. Konflikt zwischen System.Text.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 und System.Text.Json, Version=9.0.0.11, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51. System.Text.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 wurde aufgrund der primären Eigenschaft System.Text.Json, Version=9.0.0.11, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 vorgezogen. Verweise, die von "System.Text.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" abhängig sind [C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\9.0.11\ref\net9.0\System.Text.Json.dll]. C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\9.0.11\ref\net9.0\System.Text.Json.dll Elementincludes einer Projektdatei, die den Verweis "C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\9.0.11\ref\net9.0\System.Text.Json.dll" verursacht haben. C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\9.0.11\ref/net9.0/System.Text.Json.dll Verweise, die von „System.Text.Json, Version=9.0.0.11, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51“ [] abhängen oder damit vereint wurden. D:\xxx\Assemblies\yyy.dll Elementincludes einer Projektdatei, die den Verweis "D:\xxx\Assemblies\yyy.dll" verursacht haben. zzz

Similar warnings appear for several other packages like System.IO.Pipelines. We've never had issues with these at run time though.

What I think:

I was wondering where Solution 2 got the need to load System.Text.Json version 9.0 from as this version is not explicitly referenced anywhere. When I expand the list of dependencies in the solution explorer, expand Frameworks and then Microsoft.NETCore.App, I can see a reference to System.Text.Json version 9.0. So, to me, this looks like .NET 9 itself is referencing a version of the package that is not up to date. And now this causes version issues with the most recent version of the package I installed from NuGet manager?

We would really appreciate any help in this respect! If how we reference external Assemblies in Solution 2 is not how you're supposed to do it, please feel free to give us a hint about how to improve that.

Developer technologies | .NET | .NET Runtime
0 comments No comments
{count} votes

Answer accepted by question author
  1. Danny Nguyen (WICLOUD CORPORATION) 5,065 Reputation points Microsoft External Staff Moderator
    2025-11-13T07:39:04.9566667+00:00

    Hi,

    From your description, changing package versions in both solutions only “works” temporarily because you’re manually shuffling assemblies instead of letting the build system resolve them properly. Manual copying of compiled framework/BCL DLLs causes your net472 libraries to embed a reference the .NET 9 runtime cannot satisfy. Modern .NET does not apply binding redirects to core assemblies as exact assembly version must match.

    Key distinction:

    NuGet package version 9.0.11 ≠ assembly version 9.0.0.11. Official builds keep the assembly version at 9.0.0.0. So a request for 9.0.0.11 indicates a nonstandard or stale copy was used when compiling.


    Verify this first: Inspect a failing library from Solution 1 (replace path):

    
    dotnet tool install --global dotnet-ildasm   # once
    
    dotnet ildasm -t .\Assemblies\YourLibrary.dll | grep System.Text.Json
    
    
    • If you see .ver 9:0:0:11 you must rebuild that library (no fix solely in Solution 2).
    • If you see .ver 9:0:0:0 then the runtime error is due to a copied/extra System.Text.Json DLL in Solution 2—remove the copy.

    Search for stray copies:

    
    find . -name System.Text.Json.dll
    
    

    Remove any you manually placed in Solution 2.


    I suggest this approach:

    Multi‑target the libraries in Solution 1 so Solution 2 consumes a native net9.0 build (or netstandard2.0 if you prefer)—not the legacy net472 binary. Only add the System.Text.Json package for targets where it is not inbox (net472 / netstandard2.0). Let .NET 9 use the runtime’s built‑in assembly. Stop copying any System.* / Microsoft.* DLLs between solutions.

    This should achieve:

    • Eliminates the dependency on System.Text.Json, Version=9.0.0.11.
    • Lets Solution 2 reference a build compiled directly against the .NET 9 reference pack (System.Text.Json assembly version 9.0.0.0).
    • Removes the need for “version juggling.”
    • Prevents future NuGet updates from breaking runtime loading.
    
    <Project Sdk="Microsoft.NET.Sdk">
    
      <TargetFrameworks>net472;netstandard2.0;net9.0</TargetFrameworks>
    
      <!-- System.Text.Json only where not inbox -->
    
      <ItemGroup>
    
        <PackageReference Include="System.Text.Json" Version="9.0.11"
    
                          Condition="'$(TargetFramework)' != 'net9.0'"
    
                          PrivateAssets="all" />
    
      </ItemGroup>
    
    </Project>
    
    

    Notes:

    • netstandard2.0 gives broad reuse; keep net472 for existing consumers.
    • net9.0 build will NOT pull the package; it uses the runtime copy.
    • PrivateAssets="all" prevents the package from flowing transitively upward unnecessarily.

    Web App (Solution 2) Project File

    
    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
    
        <TargetFramework>net9.0</TargetFramework>
    
        <Nullable>enable</Nullable>
    
        <ImplicitUsings>enable</ImplicitUsings>
    
      </PropertyGroup>
    
      <ItemGroup>
    
        <ProjectReference Include="..\\Solution1\\YourSharedLibrary.csproj" />
    
      </ItemGroup>
    
    </Project>
    
    

    Migration Steps

    1. Update each Solution 1 library to multi‑target as shown.
    2. Remove the “Assemblies” folder copies of System.*, Microsoft.*, System.Text.Json.dll from Solution 2 (keep only proprietary company DLLs until they’re also multi‑targeted).
    3. Replace file <Reference> entries in Solution 2 with <ProjectReference>.
    4. Delete all bin and obj folders (both solutions).
    5. Rebuild.

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2025-11-12T15:42:22.7433333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    It sounds like you're facing a classic issue with versioning and assembly references in .NET, particularly with the System.Text.Json library across different solutions targeting different frameworks. Here are some steps and considerations that might help you resolve the issue:

    1. Binding Redirects: Since you are dealing with multiple versions of System.Text.Json, you might need to add binding redirects in your app.config or web.config file in Solution 2. This will instruct the runtime to redirect requests for one version of the assembly to another version. For example:
         <configuration>
           <runtime>
             <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
               <dependentAssembly>
                 <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
                 <bindingRedirect oldVersion="0.0.0.0-9.0.0.11" newVersion="9.0.0.11" />
               </dependentAssembly>
             </assemblyBinding>
           </runtime>
         </configuration>
      
      This will help ensure that the correct version is loaded at runtime.
    2. NuGet Package Management: Ensure that both solutions are using compatible versions of the System.Text.Json package. Since Solution 1 uses version 9.0.11, you should ensure that Solution 2 references the same version explicitly, even if it is included in .NET 9. This can help avoid conflicts.
    3. Clean and Rebuild: After making changes to your references or configuration files, perform a clean and rebuild of both solutions. This ensures that any cached versions of assemblies are cleared out and that the latest versions are used.
    4. Check for Transitive Dependencies: Sometimes, other NuGet packages may have dependencies on specific versions of System.Text.Json. Use the NuGet Package Manager to inspect the dependency tree and ensure that there are no conflicting versions being pulled in.
    5. Review Assembly References: In Solution 2, ensure that you are not inadvertently referencing older versions of the assemblies. Check the references in your project file (.csproj) and ensure that they point to the correct versions.
    6. Use Local Copies Wisely: While copying the DLLs to the Assemblies directory can work, it can also lead to confusion if versions are mismatched. Consider using NuGet to manage dependencies instead of manually copying DLLs, as this will help maintain consistency across projects.
    7. Runtime Configuration: If you are still encountering issues, consider using the Microsoft.Extensions.DependencyModel to inspect the runtime dependencies and ensure that the correct versions are being resolved at runtime.

    By following these steps, you should be able to mitigate the versioning issues with System.Text.Json and ensure that your applications run smoothly without runtime exceptions related to assembly loading.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.