How to cancel or stop a request initiated from UWP to Azure Mobile Apps to query data from a table?

Petchiammal Rajumayandi 61 Reputation points
2021-12-13T09:10:26.167+00:00

Hi,

We are using Azure Mobile Apps in our UWP app with .NET client library as explained in the below link
https://learn.microsoft.com/en-us/azure/developer/mobile-apps/azure-mobile-apps/howto/client/dotnet

We have a situation in our app to cancel or stop the request sent to query the data from the Table.

How to achieve this?

Universal Windows Platform (UWP)
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,243 questions
0 comments No comments
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,856 Reputation points
    2021-12-14T02:06:11.323+00:00

    Hello,
    Welcome to Microsoft Q&A!

    How to cancel or stop a request initiated from UWP

    For cancel or stop a request, you could refer to Customize request headers document. please check the custom DelegatingHandler, it has CancellationToken, you just make a property to get this token, and if you want to cancel or stop a request, just call cancel method.

    public class MyHandler : DelegatingHandler  
    {  
      
        public CancellationTokenSource currentSource;  
      
        protected override async Task<HttpResponseMessage>  
            SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)  
        {  
    
            currentSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);  
            // Change the request-side here based on the HttpRequestMessage  
            request.Headers.Add("x-my-header", "my value");  
      
            // Do the request  
            var response = await base.SendAsync(request, cancellationToken);  
            
         
            // Change the response-side here based on the HttpResponseMessage  
      
            // Return the modified response  
            return response;  
        }  
    }  
    

    Usage

    var handel = new MyHandler();  
    
     // cancel if necessary.  
    handel.currentSource?.Cancel();  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful