我需要对通过 Flurl 4.0 进行的所有调用(或至少对特定 Flurl 客户端进行的所有调用)使用 Newtonsoft JSON 序列化程序,但我不知道如何设置它。从 FLURL 4.0 use Newtonsoft JSON Serializer on a single request 问题的答案中,我了解了如何为每个调用执行此操作,但这似乎有点乏味,因为我需要对所有调用使用相同的设置。 (a) 全局设置(即不绑定到特定客户端)和 (b) 为给定客户端发出的所有请求设置它的正确方法是什么?要创建客户端,我使用以下辅助方法:
public static IFlurlClient Create
(
string url,
string? proxy = null
)
{
IFlurlClient client = string.IsNullOrEmpty(proxy)
? new FlurlClient(url)
: new FlurlClientBuilder(url)
.ConfigureInnerHandler(h => {
h.Proxy = new WebProxy(proxy);
h.UseProxy = true; })
.Build();
return client;
}
我需要向此方法注入什么才能使其与 Newtonsoft JSON 序列化程序一起使用?
首先 从 NuGet 安装 Newtonsoft 序列化器。
如果您只需要它与您在代码中创建的客户端一起使用:
client.Settings.JsonSerializer = new NewtonsoftJsonSearializer();
上面链接的 NuGet 页面上的自述文件还提到了其他几种使用它的方法:
FlurlHttp.Clients.UseNewtonsoft()
new FlurlClientCache().UseNewtonsoft()