RadzenDropDownDataGrid async LoadData and async EF Core hangs #60

Closed
opened 2026-01-29 17:30:54 +00:00 by claunia · 3 comments
Owner

Originally created by @zerox981 on GitHub (Mar 18, 2021).

Describe the bug
RadzenDropDownDataGrid with stopps to work when used as async and you call async Entity Framework methods or asnyc methods from the userManager (asp.net identity) (that uses async EF calls under the hood).
This happens only when using PostgreSQL or SQL Server as the datasource. When using Memory or SQLite it works.
The first load is successful. When you start typing and trigger the filtering it is stuck at var isAdmin = await _userManager.IsInRoleAsync(user, "admin"); in the sample, it would be also stuck at any async EF Core call ex. var someRole = await _ctx.Roles.FirstAsync();.
No exceptions are thrown.
And the whole blazor app hangs (you have to hard-refresh it)
If you run that part Synchronous it works.

@page "/experiments"
@using System.Linq.Dynamic.Core

<h3>Experiments</h3>

<RadzenDropDownDataGrid TValue="string" LoadData="@LoadData" AllowFiltering="true" AllowClear="true" AllowSorting="false" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                        Data=@employees Count="@count" TextProperty="Name" ValueProperty="Name"
/>

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Radzen;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Security.Claims;
using System.Threading.Tasks;

namespace RadzenStrangeBehaviour.Pages
{
    public partial class Experiments
    {
        [Inject] private UserManager<IdentityUser> _userManager { get; set; }
        [Inject] private IHttpContextAccessor _httpCtx { get; set; }
        private string UserId => _httpCtx.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);

        public class Emp
        {
            public string Name { get; set; }
            public string LastName { get; set; }
        }

        private int count = 0;
        private List<Emp> employees = new();

        private List<Emp> query = new List<Emp>(){
            new Emp{Name = "Andrej", LastName = "A"},
            new Emp{Name = "Adrian", LastName = "A"},
            new Emp{Name = "Alex", LastName = "E"},
            new Emp{Name = "Brane", LastName = "C"},
            new Emp{Name = "Bogdan", LastName = "D"},
            new Emp{Name = "Boio", LastName = "Z"},
            new Emp{Name = "Cene", LastName = "Y"},
            new Emp{Name = "Doris", LastName = "X"}};

        protected override async Task OnInitializedAsync()
        {
            await LoadData(new LoadDataArgs() { Skip = 0, Top = 5 });
        }

        private async Task LoadData(LoadDataArgs args)
        {
            var userId = UserId;
            var user = _userManager.Users.SingleOrDefault(u => u.Id == userId);

            var isAdmin = await _userManager.IsInRoleAsync(user, "admin"); // when you
            //var isAdmin = AsyncHelper.RunSync<bool>(() =>    _userManager.IsInRoleAsync(user, "admin") ); // <-this works
            var q = query.AsQueryable();
            if (!string.IsNullOrEmpty(args.Filter))
            {
                q = q.Where(x => x.Name.ToLower().Contains(args.Filter.ToLower()));
            }

            if (!string.IsNullOrEmpty(args.OrderBy))
            {
                q = q.OrderBy(args.OrderBy);
            }

            count = q.Count();
            employees = q.Skip(args.Skip.Value).Take(args.Top.Value).ToList();
            await InvokeAsync(StateHasChanged);
        }
    }
}

To Reproduce
A template project is created here:
https://github.com/zerox981/RadzenStrangeBehaviour
You need to change the connection string to your SQL server.
Then login with admin@local : Changeme1!
And visit /experiments & try to filter the dropdown.

Expected behavior
RadzenDropDownDataGrid would get filtered.
Blazor app would continue to function normally.

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser Chrome 89, Firefox 86.0.1
Originally created by @zerox981 on GitHub (Mar 18, 2021). <!-- IMPORTANT: Read this first!!! 1. If you own a Radzen Professional or Еnterprise subscription you can report your issue or ask us a question via email at info@radzen.com. Radzen staff will reply within 24 hours (Professional) or 16 hours (Enterprise) 2. The Radzen staff guarantees a response to issues in this repo only to paid subscribers. 3. If you have a HOW TO question start a new forum thread in the Radzen Community forum: https://forum.radzen.com. Radzen staff will close issues that are HOWTO questions. 4. Please adhere to the issue template. Specify all the steps required to reproduce the issue or link a project which reproduces it easily (without requiring extra steps such as restoring a database). --> **Describe the bug** RadzenDropDownDataGrid with stopps to work when used as async and you call async Entity Framework methods or asnyc methods from the userManager (asp.net identity) (that uses async EF calls under the hood). This happens only when using PostgreSQL or SQL Server as the datasource. When using Memory or SQLite it works. The first load is successful. When you start typing and trigger the filtering it is stuck at `var isAdmin = await _userManager.IsInRoleAsync(user, "admin");` in the sample, it would be also stuck at any async EF Core call ex. `var someRole = await _ctx.Roles.FirstAsync();`. No exceptions are thrown. And the whole blazor app hangs (you have to hard-refresh it) If you run that part Synchronous it works. ```html @page "/experiments" @using System.Linq.Dynamic.Core <h3>Experiments</h3> <RadzenDropDownDataGrid TValue="string" LoadData="@LoadData" AllowFiltering="true" AllowClear="true" AllowSorting="false" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" Data=@employees Count="@count" TextProperty="Name" ValueProperty="Name" /> ``` ```csharp using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Radzen; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; using System.Security.Claims; using System.Threading.Tasks; namespace RadzenStrangeBehaviour.Pages { public partial class Experiments { [Inject] private UserManager<IdentityUser> _userManager { get; set; } [Inject] private IHttpContextAccessor _httpCtx { get; set; } private string UserId => _httpCtx.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier); public class Emp { public string Name { get; set; } public string LastName { get; set; } } private int count = 0; private List<Emp> employees = new(); private List<Emp> query = new List<Emp>(){ new Emp{Name = "Andrej", LastName = "A"}, new Emp{Name = "Adrian", LastName = "A"}, new Emp{Name = "Alex", LastName = "E"}, new Emp{Name = "Brane", LastName = "C"}, new Emp{Name = "Bogdan", LastName = "D"}, new Emp{Name = "Boio", LastName = "Z"}, new Emp{Name = "Cene", LastName = "Y"}, new Emp{Name = "Doris", LastName = "X"}}; protected override async Task OnInitializedAsync() { await LoadData(new LoadDataArgs() { Skip = 0, Top = 5 }); } private async Task LoadData(LoadDataArgs args) { var userId = UserId; var user = _userManager.Users.SingleOrDefault(u => u.Id == userId); var isAdmin = await _userManager.IsInRoleAsync(user, "admin"); // when you //var isAdmin = AsyncHelper.RunSync<bool>(() => _userManager.IsInRoleAsync(user, "admin") ); // <-this works var q = query.AsQueryable(); if (!string.IsNullOrEmpty(args.Filter)) { q = q.Where(x => x.Name.ToLower().Contains(args.Filter.ToLower())); } if (!string.IsNullOrEmpty(args.OrderBy)) { q = q.OrderBy(args.OrderBy); } count = q.Count(); employees = q.Skip(args.Skip.Value).Take(args.Top.Value).ToList(); await InvokeAsync(StateHasChanged); } } } ``` **To Reproduce** A template project is created here: https://github.com/zerox981/RadzenStrangeBehaviour You need to change the connection string to your SQL server. Then login with admin@local : Changeme1! And visit /experiments & try to filter the dropdown. **Expected behavior** RadzenDropDownDataGrid would get filtered. Blazor app would continue to function normally. **Desktop (please complete the following information):** - OS: Windows 10 - Browser Chrome 89, Firefox 86.0.1
Author
Owner

@enchev commented on GitHub (Mar 18, 2021):

Hey @zerox981 ,

You can attach the source code to debug your application.

@enchev commented on GitHub (Mar 18, 2021): Hey @zerox981 , You can attach the source code to debug your application.
Author
Owner

@zerox981 commented on GitHub (Mar 18, 2021):

Hello @enchev ,
What do you mean, by attaching the source code?
I think this is a bug in the RadzenDropDownData component, because RadzenDataGrid with almost the same code runs fine when using async Entity Framework Core calls.
There is nothing specific in the sample application I prepared.
I thought you maybe had same issues in other components and fixed them and forgot about RadzenDropDownData.
Why exactly was this issue closed?

@zerox981 commented on GitHub (Mar 18, 2021): Hello @enchev , What do you mean, by attaching the source code? I think this is a bug in the RadzenDropDownData component, because RadzenDataGrid with almost the same code runs fine when using async Entity Framework Core calls. There is nothing specific in the sample application I prepared. I thought you maybe had same issues in other components and fixed them and forgot about RadzenDropDownData. Why exactly was this issue closed?
Author
Owner

@markolazic88 commented on GitHub (Jul 20, 2021):

We had very similar issue that took us 2 days to resolve. Initial EF call which loads the data would work fine. Then EF call that filters the data would simply never return result. UI thread would freeze waiting on that result. We made so many solution attempts on all levels, until we added our DataService call to InvokeAsync.

InvokeAsync(async () =>
            {
                var response = await DataService.GetData(args.ToQueryArgs())); // async method that invokes EF call
                // process response
                StateHasChanged();
            });

It seems that RadzenDropDownDataGrid LoadData event handler invokes DataService call on a thread that somehow doesn't return result, so main (UI) thread waits on forever. By wrapping the call in InvokeAsync we make sure that this call happens on UI thread and everything works fine.

We use Radzen.Blazor 3.5.0.

I hope this helps someone with the same issue, or it helps Radzen team to figure out what is RadzenDropDownDataGrid doing differently compared to regular DataGrid, which works fine.

@markolazic88 commented on GitHub (Jul 20, 2021): We had very similar issue that took us 2 days to resolve. Initial EF call which loads the data would work fine. Then EF call that filters the data would simply never return result. UI thread would freeze waiting on that result. We made so many solution attempts on all levels, until we added our DataService call to InvokeAsync. ``` InvokeAsync(async () => { var response = await DataService.GetData(args.ToQueryArgs())); // async method that invokes EF call // process response StateHasChanged(); }); ``` It seems that RadzenDropDownDataGrid LoadData event handler invokes DataService call on a thread that somehow doesn't return result, so main (UI) thread waits on forever. By wrapping the call in InvokeAsync we make sure that this call happens on UI thread and everything works fine. We use Radzen.Blazor 3.5.0. I hope this helps someone with the same issue, or it helps Radzen team to figure out what is RadzenDropDownDataGrid doing differently compared to regular DataGrid, which works fine.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#60