How should I implement ExecuteAsync with RestSharp on Windows Phone 7?

前端 未结 6 1444
忘掉有多难
忘掉有多难 2020-12-08 04:50

I\'m attempting to use the documentation on the RestSharp GitHub wiki to implement calls to my REST API service but I\'m having an issue with the ExecuteAsync method in part

6条回答
  •  忘掉有多难
    2020-12-08 05:22

    The following did the job

    public async Task> ExecuteAsync(IRestRequest request) where T : class, new()
    {
        var client = new RestClient
        {
            BaseUrl = _baseUrl,
            Authenticator = new HttpBasicAuthenticator(_useraname, _password),
            Timeout = 3000,
        };
    
        var tcs = new TaskCompletionSource();
        client.ExecuteAsync(request, restResponse =>
        {
            if (restResponse.ErrorException != null)
            {
                const string message = "Error retrieving response.";
                throw new ApplicationException(message, restResponse.ErrorException);
            }
            tcs.SetResult(restResponse.Data);
        });
    
        return await tcs.Task as IRestResponse;
    
    }
    

提交回复
热议问题