简体   繁体   中英

How can I set timeout in MvvmCross.Plugins.Network.Rest?

This is the code in my UserService:

var request = new MvxRestRequest("http://localhost:8080/user/auth");

var client = Mvx.Resolve<IMvxRestClient>();

MvxStreamRestResponse response = await client.MakeStreamRequestAsync(request);

messageService.showMessage(response.StatusCode.ToString()); //Never reached when my server is down

When my REST is down, I never get the control again. How can I set a Timeout or another approach in this case?

MakeStreamRequestAsync has an overload which takes a CancellationToken :

Task<MvxStreamRestResponse> MakeStreamRequestAsync(MvxRestRequest restRequest, CancellationToken cancellationToken = default(CancellationToken));

You could use it with a CancellationTokenSource and have that decide when to time out the request:

var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var response = await client.MakeStreamRequestAsync(request, cts.Token);

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