Duplicated
Last Updated: 08 Oct 2020 13:16 by ADMIN
Padhraic
Created on: 29 Mar 2018 09:33
Category: UI for ASP.NET Core
Type: Feature Request
7
[Duplicate] Kendo.Mvc.Extensions support for CancellationTokens
Even when using server side paging some requests can be very long, and may be canceled by the user.  Adding a CancellationToken parameter to the ToDataSourceResultAsync.
Duplicated
This item is a duplicate of an already existing item. You can find the original item here:
4 comments
ADMIN
Aleksandar
Posted on: 20 Mar 2020 07:09

Hi Joe,

Thank you for sharing this implementation with the community. We do appreciate the sharing of custom implementations that could be helpful for others and I am sure it would be beneficial for community members, experiencing similar issues.

Regards,
Aleksandar
Progress Telerik

Get quickly onboarded and successful with Telerik UI for ASP.NET Core with the dedicated Virtual Classroom technical training, available to all active customers.
Joe
Posted on: 13 Mar 2020 08:43

I have implemented a work-around for this issue which does the job for us.
It supports the CancellationToken, filters and sorting.

We ran into the issue that the underlying Count() request caused most of the performance issues we had on a large table. 
To resolve this the number of returned and counted items is limited to 5000 by default. When reaching the last page it will always add an extra 5000 to the total count to allow accessing more pages.

Would be good to have a similar feature out-of-the-box.

 

Here is my example: 

using System;
using System.Linq;
using System.Threading.Tasks;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System.Linq.Dynamic;
using System.Threading;
using System.Data.Entity;
namespace Test
{
    public class QueryTest
    {
        public async Task<DataSourceResult> GetPageAsync<T>(DataSourceRequest request, IQueryable<T> baseQuery,
            CancellationToken cancellationToken) where T : class, new()
        {
            var queryFiltered = baseQuery.Where(request.Filters);
            var sort = string.Empty;
            foreach (var sortDescriptor in request.Sorts)
            {
                if (string.IsNullOrWhiteSpace(sort))
                {
                    sort = sortDescriptor.Member + " " + sortDescriptor.SortDirection;
                }
                else
                {
                    sort += ", " + sortDescriptor.Member + " " + sortDescriptor.SortDirection;
                }
            }
            var query = !string.IsNullOrWhiteSpace(sort) ? queryFiltered.OrderBy(sort) : queryFiltered;
            // max of 5000 elements to reduce time for total count on large data
            int max = 5000;
            int totalItems = max;
            int take = request.PageSize;
            int skip = (request.Page - 1) * take;
            while (skip + 2 * take > totalItems)
            {
                totalItems += max;
            }
            var queryCount = queryFiltered.Cast<T>().Take(totalItems);
            var queryItems = query.Cast<T>().Skip(skip).Take(take);
            var items = await queryItems.ToListAsync(cancellationToken);
            var count = await queryCount.CountAsync(cancellationToken);
            var dsResult = new DataSourceResult()
            {
                Data = items,
                Total = count
            };
            return dsResult;
        }
    }
}
ADMIN
Aleksandar
Posted on: 12 Mar 2020 14:42

Hi Joe,

This is indeed a valid point and I agree support of CancellationToken could be useful. Based on the interest the community shows in it, we will consider its implementation in a subsequent release. 

Regards,
Aleksandar
Progress Telerik

Get quickly onboarded and successful with Telerik UI for ASP.NET Core with the dedicated Virtual Classroom technical training, available to all active customers.
Joe
Posted on: 06 Mar 2020 09:14
Depending on the filtering some of the queries in our system can take relatively long causing our impatient users to reload the page which makes the situation even worse.
Supporting the CancellationToken would ease this significantly.