Open Closed

How does CancellationTokenProvider work with Blazor Server? #6919


User avatar
0
Sturla created

I´m trying to figure out how to use the CancellationTokenProvider insted of adding CancellationToken to all my ApplicationService methods and then passing CancellationToken to them in my UI.

I might be misunderstanding how this is supposed to work but the documentation on this is serverly lacking

When navigating from the page should cancel the reqest and make IsCancellationRequested == true or do I have to do something in the UI?

Here is my code where I added a Task.Delay to the repository method to be able to then quickly navigate from that page.

Could this be related to Blazor Server? IF it doesn´t work with BS then it would be great to add that to the documentation.

UPDATE: I was also unsuccesfull passing token manually into the methods (something I was hoping I didn´t need to do)

Blazor

@inject INavigationInterception NavigationInterception

private CancellationTokenSource _cts = new();

protected override async Task OnInitializedAsync()
{
    await NavigationInterception.EnableNavigationInterceptionAsync();
    NavigationManager.LocationChanged += HandleLocationChanged;
}

private async Task OpenEditBookModalAsync(BookDto input)
{
    var book = await BooksAppService.GetAsync(input.Id, _cts.Token); // Pass CancellationToken

    EditingBookId = book.Id;
    EditingBook = ObjectMapper.Map<BookDto, BookUpdateDto>(book);
    await EditingBookValidations.ClearAll();
    await EditBookModal.Show();
}

private void HandleLocationChanged(object sender, LocationChangedEventArgs e)
{
    _cts.Cancel(); // Cancel the current operations
}

public async ValueTask DisposeAsync()
{
    NavigationManager.LocationChanged -= HandleLocationChanged;
    _cts?.Cancel();
    _cts?.Dispose();
}
public virtual async Task<PagedResultDto<BookDto>> GetListAsync(GetBooksInput input, CancellationToken cancellationToken)
{
    var totalCount = await _bookRepository.GetCountAsync(input.FilterText, input.Name, input.Email, cancellationToken);
    var items = await _bookRepository.GetListAsync(input.FilterText,
     input.Name,
      input.Email,
       input.Sorting,
        input.MaxResultCount,
         input.SkipCount,
          cancellationToken);
    return new PagedResultDto<BookDto>
    {
        TotalCount = totalCount,
        Items = ObjectMapper.Map<List<Book>, List<BookDto>>(items)
    };
}

Repository

    public abstract class EfCoreBookRepositoryBase : EfCoreRepository<CasaDbContext, Book, Guid>
    {
        public EfCoreBookRepositoryBase(IDbContextProvider<CasaDbContext> dbContextProvider)
            : base(dbContextProvider)
        {

        }

        public virtual async Task<List<Book>> GetListAsync(
            string? filterText = null,
            string? name = null,
            string? email = null,
            string? sorting = null,
            int maxResultCount = int.MaxValue,
            int skipCount = 0,
            CancellationToken cancellationToken = default)
        {
            Task.Delay(10000).Wait();
            var query = ApplyFilter((await GetQueryableAsync()), filterText, name, email);
            query = query.OrderBy(string.IsNullOrWhiteSpace(sorting) ? BookConsts.GetDefaultSorting(false) : sorting);
            return await query.PageBy(skipCount, maxResultCount).ToListAsync(cancellationToken);
        }
       }

How should this work? 🤷‍♂️🤨

  • ABP Framework version: 8.0.5
  • UI Type: Blazor Server
  • Database System: EF Core

1 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    The CancellationToken of blazor server comes from HttpContext.RequestAborted.

    But it won't work, because we'll use signalR to communication.

    ASP NET Core does not support CancellationToken in blazor server either

    https://github.com/dotnet/aspnetcore/issues/11542

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11