Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#2,402 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
65/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
csharp
Ambito
api

Direzione di ricerca

Inizia con RestClient.ConfigureSerializers e SerializerConfig.UseDefaultSerializers, quindi esamina il costruttore di SystemTextJsonSerializer indicato nella stack trace. Riproduci l'esempio net48 con System.Text.Json.dll rimosso e traccia quando viene istanziato il serializzatore predefinito. Il lavoro è completato quando un serializzatore Newtonsoft.Json configurato esplicitamente può costruire RestClient senza richiedere System.Text.Json.dll, mentre il serializzatore XML predefinito continua a funzionare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

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.

Lingua principale
C#
Stelle
9.8k
Fork
2.3k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di restsharp/RestSharp

Tutte le issue di restsharp/RestSharp

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.