0

下面是我调用的服务方法....在我调用了服务之后...我的网络断开了,我取消了令牌

这意味着在我点击服务后我取消令牌......有没有办法取消我调用的服务......

下面是我的代码

    public async Task<List<MeritData>> GetSyncupData(string Id, string BusinessDate, CancellationToken cancellationToken)
    {
        List<MeritData> PendingData = new List<MeritData>();
        HttpResponseMessage responseMessage = null;
        try
        {
            cancellationToken.ThrowIfCancellationRequested();

            var api = RestService.For<IInventoryMeritStatusApi>(_httpClient);
            responseMessage = await api.GetMeritData(Id, BusinessDate, cancellationToken).ConfigureAwait(false);

            if (cancellationToken.IsCancellationRequested)
                cancellationToken.ThrowIfCancellationRequested();

            if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
            {
               
            // my code on success

            }
        }
        catch (SocketException SocketEx)
        {
            ServiceException serviceException = new ServiceException("SocketException", SocketEx);
            throw serviceException;
        }
        catch (HttpRequestException httpReqEx)
        {
            ServiceException serviceException = new ServiceException("NoInternet", httpReqEx);
            throw serviceException;
        }
        catch (ServiceException serviceEx)
        {
            ServiceException serviceException = new ServiceException("NoInternet", serviceEx);
            throw serviceException;
        }
        catch (OperationCanceledException ex)
        {
            Console.WriteLine("TaskCanceledException GetProduct");
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return PendingData;
    }

行后

            responseMessage = await api.GetMeritData(Id, BusinessDate, cancellationToken).ConfigureAwait(false);

执行我关闭网络并调用cancellationTokenSource.Cancel();

这需要很长时间,最后得到一个“httprequestexception”而不是令牌取消异常

我该如何解决?我想在调用 token.Cancel 时取消对 web 服务的调用

4

0 回答 0