Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭
#2,402 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
65/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
csharp
领域
api

调研方向

先从 RestClient.ConfigureSerializers 和 SerializerConfig.UseDefaultSerializers 开始,然后检查堆栈跟踪中提到的 SystemTextJsonSerializer 构造函数。移除 System.Text.Json.dll 后重现 net48 示例,并跟踪默认序列化器何时被实例化。完成标准是,显式配置的 Newtonsoft.Json 序列化器可以在不要求 System.Text.Json.dll 的情况下构造 RestClient,同时默认 XML 序列化器仍能正常工作。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
C#
星标
9.8k
派生
2.3k
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

restsharp/RestSharp 的其他 Issue

查看 restsharp/RestSharp 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。