我得到的错误在返回语句的最后一行。
"无法将类型'System.Threading.Tasks.Task'隐式转换为RestSharp.IRestResponse"。显式转换退出(您是否缺少演员表?
public static async Task<IRestResponse<T>> ExecuteAsyncRequest<T>(this RestClient client, IRestRequest request) where T : class, new() //Since we used the T. We need to specify wether T is of type class or new type
{
var taskCompletionSource = new TaskCompletionSource<IRestResponse>();
client.ExecuteAsync<T>(request, restResponse =>
{
//Verbose message of the error
if (restResponse.ErrorException != null)
{
const string message = "Error retrieving response.";
throw new ApplicationException(message, restResponse.ErrorException);
}
//Setting the result of the execution
taskCompletionSource.SetResult(restResponse);
});
//return us the reuslt
return await taskCompletionSource.Task;
我无法完全重现这一点:我得到一个稍微不同的错误
无法将类型"RestSharp.IRestResponse"隐式转换为"RestSharp.IRestResponse"。显式转换退出(您是否缺少转换?
链接
这是因为您的方法返回一个IRestResponse<T>
,但您的TaskCompletionSource
只包含一个IRestResponse
。您可以通过将其更改为TaskCompletionSource<IRestResponse<T>>
来修复错误。
var taskCompletionSource = new TaskCompletionSource<IRestResponse<T>>();