RestClient configured with Newtonsoft.Json serializer still instantiates System.Text.Json
还没有人认领这个 Issue。
评估
调研方向
先从 RestClient.ConfigureSerializers 和 SerializerConfig.UseDefaultSerializers 开始,然后检查堆栈跟踪中提到的 SystemTextJsonSerializer 构造函数。移除 System.Text.Json.dll 后重现 net48 示例,并跟踪默认序列化器何时被实例化。完成标准是,显式配置的 Newtonsoft.Json 序列化器可以在不要求 System.Text.Json.dll 的情况下构造 RestClient,同时默认 XML 序列化器仍能正常工作。
由索引模型根据 Issue 内容生成。
描述
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
- 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()))
{
}
}
}
- Build the project:
dotnet build
- Remove this file from the application output directory:
bin\Debug\net48\System.Text.Json.dll
- 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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
restsharp/RestSharp 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
-
delete 940 未关闭
难度 1/5 1 小时以内 新手友好度 35/100
-
js/calculator.js 未关闭
难度 5/5 一周以上 新手友好度 10/100
-
index.html 未关闭
难度 5/5 一周以上 新手友好度 15/100
-
难度 5/5 一周以上 新手友好度 1/100
查看 restsharp/RestSharp 的全部 Issue
相似的 Issue
-
core dependencies
难度 1/5 1 小时以内 新手友好度 80/100
-
bug frontend good first issue
难度 2/5 1-3 小时 新手友好度 75/100
-
NavigationViewItemAutomationPeer implements IInvokeProvider but never advertises the Invoke pattern 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
unoplatform/uno#24629 ·
-
agentic-workflows Needs: Triage :mag: State: In-PR
难度 2/5 1-3 小时 新手友好度 70/100
-
Down / Waiting for removal
难度 2/5 1-3 小时 新手友好度 70/100