简体   繁体   中英

RestSharp synchronous request in windows phone

Is there a way (any, no matter how) to simulate synchronous requests using restsharp?

I'm developing an app which has to wait for a successful login response to navigate forward, it is a pain to be passing callbacks all over my code just to check stuff.

Use the Microsoft.Bcl.Async package .

Then with an extension method like:

public static class RestClientExtensions
    {
        public static Task<IRestResponse> ExecuteTask (this IRestClient restClient, RestRequest restRequest)
        {
            var tcs = new TaskCompletionSource<IRestResponse> ();
            restClient.ExecuteAsync (restRequest, (restResponse, asyncHandle) =>
            {
                if (restResponse.ResponseStatus == ResponseStatus.Error)
                    tcs.SetException (restResponse.ErrorException);
                else
                    tcs.SetResult (restResponse);
            });
            return tcs.Task;
        }
    }

You can make calls like:

var restResponse = await restClient.ExecuteTask(restRequest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM