在创建,使用和处理多个HttpClient时,我注意到在TIME_WAIT状态下仍有套接字处于打开状态.
例如,运行以下代码后:
using System.Net.Http;
namespace HttpClientTest
{
public class Program
{
public static void Main(string[] args)
{
for (var i = 0; i < 10; i++)
{
using (var httpClient = new HttpClient())
{
var result = httpClient.
GetAsync("http://stackoverflow.com/").
Result;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到netstat,套接字是打开的:
TCP 10.200.60.168:2722 151.101.193.69:http TIME_WAIT
TCP 10.200.60.168:2751 151.101.193.69:http TIME_WAIT
TCP 10.200.60.168:2752 151.101.193.69:http TIME_WAIT
TCP 10.200.60.168:2753 151.101.193.69:http TIME_WAIT
TCP 10.200.60.168:2754 151.101.193.69:http TIME_WAIT
TCP 10.200.60.168:2755 151.101.193.69:http TIME_WAIT
TCP 10.200.60.168:2756 151.101.193.69:http TIME_WAIT
TCP 10.200.60.168:2757 151.101.193.69:http TIME_WAIT
TCP 10.200.60.168:2758 …
Run Code Online (Sandbox Code Playgroud) 我需要使用 Json.NET 序列化许多不同的对象。我真的无法控制所提供的对象,所以我通常使用TypeNameHandling.All进行序列化和反序列化。
但是,其中一些对象无法反序列化。具体来说,我得到了一些 System.Reflection.RuntimePropertyInfo 类型。我想以标准化的方式处理这些,因为在反序列化时我不知道目标类型。我也不关心,只要输出对象类型是正确的。
我尝试将CustomCreationConverter输入到在JsonSerializerSettings 中定义的PropertyInfo。但是,即使 CanConvert() 返回 true,也从未使用 CustomCreationConverter 的 ReadJson()。
最终结果就像我从未使用过 CustomCreationConverter 一样:
ISerializable 类型“System.Reflection.RuntimePropertyInfo”没有有效的构造函数。要正确实现 ISerializable,应存在采用 SerializationInfo 和 StreamingContext 参数的构造函数。
我需要 CustomCreationConverter 来处理 ReadJson,以便我可以自己手动搜索 PropertyInfo。
经过更多调查,似乎我添加到 JsonSerializerSettings 的转换器根本没有被使用。如果我使用包含 JsonConverter 的类型和集合的 DeserializeObject 重载,则将使用转换器。我不确定提供给 JsonSerializerSettings 的转换器的用途是什么,但我希望它们能在这种情况下按我的意图工作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
namespace Json
{
class Program
{
static void Main(string[] args)
{
var jsonSerializerSettings = …
Run Code Online (Sandbox Code Playgroud)