Skip to content

RestClient configured with Newtonsoft.Json serializer still instantiates System.Text.Json #2402

Description

@segor

Describe the bug

RestClient creates its default SystemTextJsonSerializer before applying the caller's configureSerialization callback.

This happens even when the application explicitly configures RestSharp.Serializers.NewtonsoftJson and does not use System.Text.Json for RestSharp serialization or deserialization.

UseDefaultSerializers() configures both SystemTextJsonSerializer and XmlRestSerializer. The XML default is not a problem here. The issue is that the built-in JSON serializer is created before the custom Newtonsoft.Json serializer can replace it.

This requires .NET Framework applications to deploy and load System.Text.Json even when they intentionally use Newtonsoft.Json exclusively.

To Reproduce

  1. Create a folder and add these two files.

RestSharpNewtonsoftRepro.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net48</TargetFramework>
    <ImplicitUsings>disable</ImplicitUsings>
    <Nullable>disable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="RestSharp" Version="114.0.0" />
    <PackageReference Include="RestSharp.Serializers.NewtonsoftJson"
                      Version="114.0.0" />
  </ItemGroup>
</Project>

Program.cs:

using RestSharp;
using RestSharp.Serializers.NewtonsoftJson;

internal class Program
{
    private static void Main()
    {
        using (var client = new RestClient(
                   new RestClientOptions("https://example.invalid"),
                   configureSerialization: serializer =>
                       serializer.UseNewtonsoftJson()))
        {
        }
    }
}
  1. Build the project:
dotnet build
  1. Remove this file from the application output directory:
bin\Debug\net48\System.Text.Json.dll
  1. Run the generated executable.

Actual behavior

The application fails while constructing RestClient, before making a request or using JSON serialization.

RestSharp creates SystemTextJsonSerializer and its System.Text.Json.JsonSerializerOptions before the Newtonsoft.Json callback is applied. Therefore, System.Text.Json is required even though the configured serializer is Newtonsoft.Json.

Expected behavior

When the caller explicitly configures a JSON serializer, RestSharp should not instantiate the built-in SystemTextJsonSerializer.

For this example, the following should construct RestClient without requiring System.Text.Json.dll:

new RestClient(
    new RestClientOptions("https://example.invalid"),
    configureSerialization: serializer => serializer.UseNewtonsoftJson());

The default XML serializer may remain configured.

An alternative explicit API to disable the default JSON serializer would also solve the problem. The important behavior is that a custom JSON serializer must be able to replace the default before SystemTextJsonSerializer is created.

Stack trace

System.IO.FileNotFoundException: Could not load file or assembly
'System.Text.Json, Version=10.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.

   at RestSharp.Serializers.Json.SystemTextJsonSerializer..ctor()
   at RestSharp.Serializers.SerializerConfig.UseDefaultSerializers()
   at RestSharp.RestClient.ConfigureSerializers(...)
   at RestSharp.RestClient..ctor(...)
   at Program.Main()

Desktop

  • OS: Windows 11
  • .NET version: .NET Framework 4.8
  • RestSharp version: 114.0.0
  • RestSharp.Serializers.NewtonsoftJson version: 114.0.0

Additional context

This is particularly problematic for plugins loaded into a shared .NET Framework AppDomain. The host or another plugin can load a different strong-named System.Text.Json version first.

An AssemblyResolve handler is not a reliable solution: it is invoked only for failed binds and cannot override an already satisfied host binding or binding redirect.

The issue started when upgrading from RestSharp 112.1.0 to 114.0.0. RestSharp 113 upgraded System.Text.Json to version 10 for all target frameworks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions