Xamarin.forms 在Xamarin窗体中网络关闭时取消服务任务
只有一项任务被取消,其余任务从未取消 我做错了什么 我的网络关闭时需要取消所有任务…Xamarin.forms 在Xamarin窗体中网络关闭时取消服务任务,xamarin.forms,offline,cancellationtokensource,Xamarin.forms,Offline,Cancellationtokensource,只有一项任务被取消,其余任务从未取消 我做错了什么 我的网络关闭时需要取消所有任务…公共异步任务GetInventoryLineItems(string Restaurantid、string FromDate、CancellationToken ct) masterDataSyncCancellationToken.Cancel(); { InvServiceModel InvServiceModel=null; HttpResponseMessage responseMessage=nul
公共异步任务GetInventoryLineItems(string Restaurantid、string FromDate、CancellationToken ct)
masterDataSyncCancellationToken.Cancel();
{
InvServiceModel InvServiceModel=null;
HttpResponseMessage responseMessage=null;
尝试
{
var api=RestService.For(_httpClient);
responseMessage=await api.GetInventoryLineItems(Restaurantid,FromDate)。ConfigureAwait(false);
if(responseMessage.StatusCode==System.Net.HttpStatusCode.OK)
{
string blobValidateResponse=await responseMessage.Content.ReadAsStringAsync();
invservicemodel=JsonConvert.DeserializeObject(blobValidateResponse,新的JsonSerializerSettings{TypeNameHandling=TypeNameHandling.None,NullValueHandling=NullValueHandling.Ignore});
}
其他的
{
analytics?.LogException(string.Format(ServiceConstants.ServiceException,responseMessage.StatusCode.GetHashCode().ToString())+
responseMessage.reasonPhase)、serviceException、nameof(GetInventoryLineItems)、nameof(InventoryLineItems));
抛出serviceException;
}
}
捕获(例外情况除外)
{
掷骰子;
}
返回模型;
}
公共异步任务GetInventoryLineItems(string Restaurantid、string FromDate、CancellationToken ct)
{
InvServiceModel InvServiceModel=null;
HttpResponseMessage responseMessage=null;
尝试
{
var api=RestService.For(_httpClient);
responseMessage=await api.GetInventoryLineItems(Restaurantid,FromDate)。ConfigureAwait(false);
if(responseMessage.StatusCode==System.Net.HttpStatusCode.OK)
{
string blobValidateResponse=await responseMessage.Content.ReadAsStringAsync();
invservicemodel=JsonConvert.DeserializeObject(blobValidateResponse,新的JsonSerializerSettings{TypeNameHandling=TypeNameHandling.None,NullValueHandling=NullValueHandling.Ignore});
}
其他的
{
analytics?.LogException(string.Format(ServiceConstants.ServiceException,responseMessage.StatusCode.GetHashCode().ToString())+
responseMessage.reasonPhase)、serviceException、nameof(GetInventoryLineItems)、nameof(InventoryLineItems));
抛出serviceException;
}
}
捕获(例外情况除外)
{
掷骰子;
}
返回模型;
}
您正在使用Refit调用Api,但您的接口没有取消令牌作为参数
例如,对于以下接口,IInventoryLineItemsApi,方法GetInventoryLineItems签名应如下所示:
public async Task<InvServiceModel> GetInventoryLineItems(string Restaurantid, string FromDate, CancellationToken ct)
{
InvServiceModel invservicemodel = null;
HttpResponseMessage responseMessage = null;
try
{
var api = RestService.For<IInventoryLineItemsApi>(_httpClient);
responseMessage = await api.GetInventoryLineItems(Restaurantid, FromDate).ConfigureAwait(false);
if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
{
string blobValidateResponse = await responseMessage.Content.ReadAsStringAsync();
invservicemodel = JsonConvert.DeserializeObject<InvServiceModel>(blobValidateResponse, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None, NullValueHandling = NullValueHandling.Ignore });
}
else
{
analytics?.LogException(string.Format(ServiceConstants.ServiceException, responseMessage.StatusCode.GetHashCode().ToString() +
responseMessage.ReasonPhrase), serviceException, nameof(GetInventoryLineItems), nameof(InventoryLineItems));
throw serviceException;
}
}
catch (Exception ex)
{
throw ex;
}
return invservicemodel;
}
这样,您的取消令牌将被传递到底层HttpClient,因此无论何时调用cancel,您的http调用都将被取消。您正在使用Refit调用Api,但您的接口没有取消令牌作为参数 例如,对于以下接口,IInventoryLineItemsApi,方法GetInventoryLineItems签名应如下所示:
public async Task<InvServiceModel> GetInventoryLineItems(string Restaurantid, string FromDate, CancellationToken ct)
{
InvServiceModel invservicemodel = null;
HttpResponseMessage responseMessage = null;
try
{
var api = RestService.For<IInventoryLineItemsApi>(_httpClient);
responseMessage = await api.GetInventoryLineItems(Restaurantid, FromDate).ConfigureAwait(false);
if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
{
string blobValidateResponse = await responseMessage.Content.ReadAsStringAsync();
invservicemodel = JsonConvert.DeserializeObject<InvServiceModel>(blobValidateResponse, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None, NullValueHandling = NullValueHandling.Ignore });
}
else
{
analytics?.LogException(string.Format(ServiceConstants.ServiceException, responseMessage.StatusCode.GetHashCode().ToString() +
responseMessage.ReasonPhrase), serviceException, nameof(GetInventoryLineItems), nameof(InventoryLineItems));
throw serviceException;
}
}
catch (Exception ex)
{
throw ex;
}
return invservicemodel;
}
这样,您的取消令牌将被传递到底层HttpClient,因此无论何时调用cancel,您的http调用都将被取消。您必须将取消令牌传递给每个异步方法,每个方法都应该将其作为一个输入,并在每个异步方法调用中传递。下面的方法中的取消过程是什么,它击中了一个web服务@akashkava您必须将取消令牌传递给每个异步方法,每个方法都应该将其作为输入,并在每个异步方法调用中传递它。下面的方法中的取消过程是什么,它击中了web服务@akashkava?我如何取消此web服务调用?我如何取消此web服务调用?
public async Task<InvServiceModel> GetInventoryLineItems(string Restaurantid, string FromDate, CancellationToken ct)
{
InvServiceModel invservicemodel = null;
HttpResponseMessage responseMessage = null;
try
{
var api = RestService.For<IInventoryLineItemsApi>(_httpClient);
responseMessage = await api.GetInventoryLineItems(Restaurantid, FromDate).ConfigureAwait(false);
if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
{
string blobValidateResponse = await responseMessage.Content.ReadAsStringAsync();
invservicemodel = JsonConvert.DeserializeObject<InvServiceModel>(blobValidateResponse, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None, NullValueHandling = NullValueHandling.Ignore });
}
else
{
analytics?.LogException(string.Format(ServiceConstants.ServiceException, responseMessage.StatusCode.GetHashCode().ToString() +
responseMessage.ReasonPhrase), serviceException, nameof(GetInventoryLineItems), nameof(InventoryLineItems));
throw serviceException;
}
}
catch (Exception ex)
{
throw ex;
}
return invservicemodel;
}
GetInventoryLineItems(string restaurantId, string fromDate, CancellationToken ct);