RestSharp.RestClient忽略超时时间限制。

6
我正在使用RestSharp.RestClient (105.2.4-rc4-24214-01)开发一个.net core应用程序。 我设置了

标签。
RestClient.Timeout=1 

或者
RestClient.Timeout=10000

然后调用我的测试API

var tcs = new TaskCompletionSource<IRestResponse>();
RestClient.ExecuteAsync(request, response => { tcs.SetResult(response); })
return tcs.Task.Result;

但它仍然使用默认值100000并且只有在100000毫秒后才生成“任务已取消”的异常。

我该如何更改这个值?


您是否在设置用于发送请求的特定RestClient实例的超时时间? - 108
是的,我确定我是。 - Timur Lemeshko
我也遇到了同样的问题,我设置了Timeout和ReadWriteTimeout,但它并没有起作用。 - Darxis
同样的问题... 唉... - willem
这个测试通过了 https://github.com/restsharp/RestSharp/blob/develop/RestSharp.IntegrationTests/AsyncTests.cs#L200 你有什么不同的做法吗? - Alexey Zimarev
2个回答

2
文档说明Request.Timeout会覆盖RestClient.Timeout。请尝试以下操作:
var tcs = new TaskCompletionSource<IRestResponse>();
request.Timeout = 10000;
RestClient.ExecuteAsync(request, response => { tcs.SetResult(response); })
return tcs.Task.Result;

0
你可以使用 CancellationTokenSource 并使用它的 token 来中止响应的处理。 请尝试以下代码以设置 10 秒超时。
System.Threading.CancellationTokenSource cts = new  System.Threading.CancellationTokenSource ();
 cts.CancelAfter (TimeSpan.FromMilliseconds(10000));
var tcs = new TaskCompletionSource<IRestResponse>();
 RestRequestAsyncHandle handle = RestClient.ExecuteAsync(request, response => { tcs.SetResult(response); })
 using (cts.Token.Register(() => handle.Abort()))
        {
        return (RestResponse)(await tcs.Task);
        }

希望能对你有所帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接