C# RestSharp ASYNC client.ExecuteAsync<T> () 工作示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12232653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:32:58  来源:igfitidea点击:

Example of RestSharp ASYNC client.ExecuteAsync<T> () works

c#restsharp

提问by Nil Pun

Could someone please help me modify the code below:

有人可以帮我修改下面的代码:

client.ExecuteAsync(request, response => {
    Console.WriteLine(response.Content);
});

Basically I want to use ExecuteAsync method above but don't want to print but return response.Content to the caller.

基本上我想使用上面的 ExecuteAsync 方法但不想打印但将 response.Content 返回给调用者。

Is there any easy way to achieve this?

有什么简单的方法可以实现这一目标吗?

I tried this but doesnt' work:

我试过这个,但不工作:

    public T Execute<T>(RestRequest request) where T : new()
        {
            var client = new RestClient();
            client.BaseUrl = BaseUrl;
            client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
            request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment); // used on every request
            var response = client.ExecuteAsync(request, response => {
    return response.data);
});

}

}

The above code is from https://github.com/restsharp/RestSharp

以上代码来自 https://github.com/restsharp/RestSharp

回答by spender

There's the thing... you can't return an asynchronously delivered value, because your calling method will already have returned. Blocking the caller until you have a result defeats the point of using ExecuteAsync. In this case, I'd return a Task<string>(assuming response.Content is a string):

问题是……你不能返回一个异步传递的值,因为你的调用方法已经返回了。在获得结果之前阻止调用者失去了使用 ExecuteAsync 的意义。在这种情况下,我会返回一个Task<string>(假设 response.Content 是一个字符串):

Task<string> GetResponseContentAsync(...)
{
  var tcs=new TaskCompletionSource<string>();
  client.ExecuteAsync(request, response => {
    tcs.SetResult(response.Content);
  });
  return tcs.Task;
}

Now, when the task completes, you have a value. As we move to c#5 async/await, you should get used to stating asynchrony in terms of Task<T>as it's pretty core.

现在,当任务完成时,您就有了价值。当我们转向 c#5 async/await 时,您应该习惯于将异步表述Task<T>为非常核心的内容。

http://msdn.microsoft.com/en-us/library/dd537609.aspx

http://msdn.microsoft.com/en-us/library/dd537609.aspx

http://msdn.microsoft.com/en-us/library/hh191443.aspx

http://msdn.microsoft.com/en-us/library/hh191443.aspx

回答by Chris Shain

From reading the code it looks like you want to use ExecuteAsGetor ExecuteAsPostinstead of the async implementation.

从阅读代码来看,您似乎想使用ExecuteAsGetExecuteAsPost代替异步实现。

Or maybe just Execute- not sure exactly what type Client is.

或者可能只是执行 - 不确定客户端是什么类型。

回答by Boris

With the help of @spender, this is what i got:

在@spender 的帮助下,这就是我得到的:

You can add new file in RestSharp project, and add this code:

您可以在 RestSharp 项目中添加新文件,并添加以下代码:

public partial class RestClient
{
    public Task<IRestResponse<T>> ExecuteAsync<T>(IRestRequest request)
    {
        var tcs=new TaskCompletionSource<IRestResponse<T>>();

        this.ExecuteAsync(request, response => 
            {
                tcs.SetResult(
                    Deserialize<T>(request, response)
                );
            });

    return tcs.Task;
    }       
}

This will practically return the full response, with status code and everything, so you can check if the status of the response is OK before getting the content, and you can get the content with:

这实际上将返回完整的响应,带有状态代码和所有内容,因此您可以在获取内容之前检查响应的状态是否正常,并且您可以通过以下方式获取内容:

response.Content