我使用RestSharp异步执行GET请求,但它永远不会结束。如果我做同样的请求同步它成功结束。 我以前就像这样使用过RestSharp而且它总能工作。
我做错了什么?
这里是异步方法:( this.Client
只是localhost)
public async Task<ServiceResponse> UpvoteArticleAsync(string source, string url)
{
var client = this.Client;
var request = new RestRequest("service/upvote/"+source+"/"+url, Method.GET) { RequestFormat = DataFormat.Json };
var cancellationTokenSource = new CancellationTokenSource();
var deserial = new JsonDeserializer();
var response = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);
return deserial.Deserialize<ServiceResponse>(response);
}
这就是我所说的:
ServiceResponse result = await rest.UpvoteArticleAsync (this.article.Source, this.article.Url);
正如我所说,如果我将异步方法更改为同步方法,它可以工作:
public async Task<ServiceResponse> UpvoteArticleAsync(string source, string url)
{
var client = this.Client;
var request = new RestRequest("service/upvote/"+source+"/"+url, Method.GET) { RequestFormat = DataFormat.Json };
var cancellationTokenSource = new CancellationTokenSource();
var deserial = new JsonDeserializer();
var response = client.ExecuteTaskAsync(request, cancellationTokenSource.Token);
return deserial.Deserialize<ServiceResponse>(response.Result);
}
修改
我用try和catch来查看是否有任何异常,但它没有抛出任何异常。
编辑2:
即使我对来自HttpWebRequest
的{{1}}做同样的请求,但在使用System.Net
的异步版本时仍然会遇到问题:
GetResponse