RestSharp超时工作不
问题描述:
我有一个restsharp客户端,并要求设立这样的:RestSharp超时工作不
var request = new RestRequest();
request.Method = Method.POST;
request.AddParameter("application/json", jsonBody, ParameterType.RequestBody);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
request.Timeout = -1;
request.ReadWriteTimeout = -1;
var url = $"http://{ipAddress}/api/calculate";
var client = new RestClient();
client.BaseUrl = new Uri(url);
client.Timeout = -1;
client.ReadWriteTimeout = -1;
var response = client.Execute(request);
该请求将需要一段时间才能完成,大约30分钟。现在,我知道有这样做的更优雅的方式,但是,对于这个请求,我需要这样做。
此RestSharp客户端和请求在Windows服务中执行。当服务执行请求时,它会抛出TimoutException并且请求最大超时大约是40秒。
出于某种原因,我设置的超时不适用于这种情况。
有人有这方面的解决方案?
答
您可能不会通过设置ReadWriteTimeout
值来做你认为你的想法。你的价值被忽略,所以你得到了默认值。
根据此回答What is default timeout value of RestSharp RestClient? RestSharp在其实现中使用HttpWebRequest
。
HttpWebRequest
的超时属性不能为负数HttpWebRequest.Timeout Property。
如果在RestSharp客户端代码看你看这个:https://github.com/restsharp/RestSharp/blob/70de357b0b9dfc3926c95d1e69967c7a7cbe874c/RestSharp/RestClient.cs#L452
int readWriteTimeout = request.ReadWriteTimeout > 0
? request.ReadWriteTimeout
: this.ReadWriteTimeout;
if (readWriteTimeout > 0)
{
http.ReadWriteTimeout = readWriteTimeout;
}
谢谢你的答案。那么,你有什么建议我做无限的时间超时? –
首先,我会将操作分解为开始,状态和结果操作。拨打电话,返回一个令牌,然后检查状态。最后,获取保存的结果。如果这是不可行的,那么我会建议尽可能长时间超时,并希望最好。如果你控制API,那就去找门#1后面的东西。 –
你说得对。在官方文档中,有一个-1表示无限制超时。我把它像Int32.MaxValue和工作。谢谢你,小伙伴! –