Base solution for your next web application
Open Closed

Cancellation Token #12336


User avatar
0
Hellonote created

What is your product version? = API: v11.3.0 | Client: v11.3.0
What is your product type? = Angular
What is product framework type? = .net core

Hello Team,
We are in need to implement cancellation token. Say, if we are on a page having long running APIs. User navigated to another page before getting response from the API from previous page. So, in this case previous page requests get cancelled gracefully.

We are trying to implement Cancellation Token. For this we created a middleware in Web.Core project and created one interceptor to add cancellation on angular project.
But this not seems working.

Do you have something on this to help us, please?


3 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @Hellonote

    Sorry for the late answer. This code defines an Angular HTTP interceptor to handle request cancellations. It uses a Subject to emit cancellation signals. The intercept method pipes HTTP requests through takeUntil, which listens for the cancellation signal. The cancelPendingRequests method triggers the cancellation of all pending requests. You need to ensure that the interceptor is used in the service or component where you make the HTTP requests. Could you please check if this resolves your issue? If this interceptor does not solve your problem, could you provide more details?

    import { Injectable } from '@angular/core';
    import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
    import { Observable, Subject } from 'rxjs';
    import { takeUntil } from 'rxjs/operators';
    
    @Injectable()
    export class CancellationInterceptor implements HttpInterceptor {
        private cancelPendingRequests$ = new Subject();
    
        intercept(req: HttpRequest, next: HttpHandler): Observable> {
            return next.handle(req).pipe(
                takeUntil(this.cancelPendingRequests$)
            );
        }
    
        cancelPendingRequests() {
            this.cancelPendingRequests$.next();
        }
    }
    
    providers: [
            { provide: HTTP_INTERCEPTORS, useClass: CancellationInterceptor, multi: true },
            // other providers
    ],
    
  • User Avatar
    0
    Hellonote created

    Hi @Hellonote

    Sorry for the late answer. This code defines an Angular HTTP interceptor to handle request cancellations. It uses a Subject to emit cancellation signals. The intercept method pipes HTTP requests through takeUntil, which listens for the cancellation signal. The cancelPendingRequests method triggers the cancellation of all pending requests. You need to ensure that the interceptor is used in the service or component where you make the HTTP requests. Could you please check if this resolves your issue? If this interceptor does not solve your problem, could you provide more details?

    import { Injectable } from '@angular/core'; 
    import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; 
    import { Observable, Subject } from 'rxjs'; 
    import { takeUntil } from 'rxjs/operators'; 
     
    @Injectable() 
    export class CancellationInterceptor implements HttpInterceptor { 
        private cancelPendingRequests$ = new Subject(); 
     
        intercept(req: HttpRequest, next: HttpHandler): Observable> { 
            return next.handle(req).pipe( 
                takeUntil(this.cancelPendingRequests$) 
            ); 
        } 
     
        cancelPendingRequests() { 
            this.cancelPendingRequests$.next(); 
        } 
    } 
    
    providers: [ 
            { provide: HTTP_INTERCEPTORS, useClass: CancellationInterceptor, multi: true }, 
            // other providers 
    ], 
    

    Hello Team,

    I already done this, with this on API side we are getting unhandled exceptions logs.
    Do we need to do something on API side as well?
    Kindly let us know with sample code if it need to handle on API side

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Could you share the error logs ?