New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
webrequest的问题 #117
Comments
webrequest的用法我用了好几年了,但是底层实现被微软替换没替换我不知道了。 |
我按如下进行了测试。用factory创建的就不会有问题。直接用using httpclient的可能在1 、2 万请求后就会爆出耗尽套接字 builder.Services.AddHttpClient();
var app = builder.Build();
{
var urls = new List<string>() { "http://localhost:17080", "http://localhost:17081" };
for (int i = 0; i < 100000; ++i)
{
using (var scope = app.Services.CreateScope())
{
var factory = scope.ServiceProvider.GetService<IHttpClientFactory>();
var c = factory.CreateClient();
var url = urls[i % 2];
var u = $"{url}?id={new Random().Next(100000)}";
var r = await c.GetAsync(u);
var content = await r.Content.ReadAsStringAsync();
Console.WriteLine($"第{i}次 返回{content}");
}
//using (var c = new HttpClient())
//{
// var url = urls[i % 2];
// var u = $"{url}?id={new Random().Next(100000)}";
// var r = await c.GetAsync(u);
// var content = await r.Content.ReadAsStringAsync();
// Console.WriteLine($"第{i}次 返回{content}");
//}
}
} |
确实,微软已经不推荐直接使用 httpwebrequest 这个类了,推荐使用 httpclient 来代替。 |
微软反复横跳,哈哈 |
项目中多处使用到了webrequest
我之前看到资料说webrequest底层现在自动使用了Httpclient, 因此是否可能使用httpclient后资源无法及时释放的问题。这点我也不确定。
是否考虑用现成的比如restsharp库
The text was updated successfully, but these errors were encountered: