这是我的用户服务中的代码:
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
当我的 REST 关闭时,我再也无法获得控制权。在这种情况下,如何设置超时或其他方法?
MakeStreamRequestAsync
有一个重载,需要CancellationToken
:
Task<MvxStreamRestResponse> MakeStreamRequestAsync(MvxRestRequest restRequest, CancellationToken cancellationToken = default(CancellationToken));
您可以将其与CancellationTokenSource
一起使用,并让它决定何时使请求超时:
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var response = await client.MakeStreamRequestAsync(request, cts.Token);