restsharp:任务被取消,请求中止(问题也发生在httpclient上)

问题描述 投票:0回答:2

有人可以帮忙确定造成这种情况的原因吗?使用restsharp我收到taskcancelledException:任务被取消。这是一个使用 .net 4.6.1 的 console.app。

在课堂程序中,我创建了两个“RestClient”。

static RestClientOptions optionsLocal = new RestClientOptions(webserverLocal)
{
    ThrowOnAnyError = true,
    Timeout = 60000
};
static RestClient myRestClientLocal = new RestClient(optionsLocal);

static RestClientOptions optionsCloud = new RestClientOptions(webserverCloud)
{
    ThrowOnAnyError = true,
    Timeout = 60000
};
static RestClient myRestClientCloud = new RestClient(optionsCloud);

然后在代码中,这个函数大约每 30 秒调用一次......

var request = new RestRequest("api/user/UM").AddJsonBody(tempObject);
var responseLocal = await myRestClientLocal.PostAsync<string>(request);
var responseCloud = await myRestClientCloud.PostAsync<string>(request);

它工作了一段时间(大约2小时),然后我的任务被取消了。有人看到我做错了什么吗?

谢谢你!!!

我以前使用过httpclient,但遇到了同样的错误,所以我尝试切换到restsharp

httpclient restsharp
2个回答
0
投票

RestClientOptions.Timeout
直接传递给 RestSharp 使用的
HttpClient
的包装实例。这就是为什么您会观察到相同的行为。

RestClientOptions
还具有
MaxTimeout
属性,该属性强制 RestSharp 设置具有指定超时的外部取消令牌源。无论超时处理如何,它都会在超时到期时取消请求。
    


0
投票

这不是OP的问题,但肯定是我的问题!

© www.soinside.com 2019 - 2024. All rights reserved.