dotnet/aspnetcore

TestServer.CreateClient : the HttpClient created didn't support well the cancellation.

Open

#5,938 创建于 2018年9月17日

在 GitHub 查看
 (4 评论) (1 反应) (0 负责人)C# (10,653 fork)batch import
affected-fewarea-hostingarea-networkingbughelp wantedseverity-minor

仓库指标

Star
 (37,933 star)
PR 合并指标
 (平均合并 16天 9小时) (30 天内合并 258 个 PR)

描述

Hello,

I just found a nasty bug on HttpClient created from TestServer. When I call a request which took 2 seconds if I cancel it after one second. The client created by test server didn't throw an OperationCancelledException.

Here the code with real httpClient :

var httpClient = new HttpClient();
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1)))
{
    var request = new HttpRequestMessage
    {
        Method = HttpMethod.Get,
        RequestUri = new Uri("http://localhost:4242/TimeoutTest/Action2Secs")
     };
     await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, cts.Token);
}

The same code with client created by the class TestServer.

var httpClient = new testServer.CreateClient();
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1)))
{
    var request = new HttpRequestMessage
    {
        Method = HttpMethod.Get,
        RequestUri = new Uri("http://localhost:4242/TimeoutTest/Action2Secs")
     };
     await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, cts.Token);
}

贡献者指南