dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.44k stars 10.02k forks source link

QuickGrid throws InvokeAsync exception when RefreshDataAsync() is called #58794

Open ViRuSTriNiTy opened 10 hours ago

ViRuSTriNiTy commented 10 hours ago

Is there an existing issue for this?

Describe the bug

Hi there,

as the title already states, QuickGrid possibly throws an InvokeAsync exception when RefreshDataAsync() is invoked from a callback associated with a background service, like a timer.

This is caused by a call to StateHasChanged() within RefreshDataAsync() without wrapping it in InvokeAsnyc as described in https://learn.microsoft.com/en-us/aspnet/core/blazor/components/synchronization-context?view=aspnetcore-8.0#invoke-component-methods-externally-to-update-state.

My current solution is to catch the exception and call StateHasChanged() with InvokeAsync otherwise rethrow:

try
{
    await Grid.RefreshDataAsync();
}
catch (InvalidOperationException ex)
{
    if (ex.Message.Contains("InvokeAsync", StringComparison.Ordinal))
        await InvokeAsync(StateHasChanged);
    else
        throw;
}

Expected Behavior

It would be nice to have await InvokeAsync(StateHasChanged) calls in QuickGrid to avoid the need to use try catches here and there.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

javiercn commented 8 hours ago

@ViRuSTriNiTy thanks for contacting us.

It's not clear what you are asking. Are you suggesting that RefreshDataAsync internally dispatches the call via InvokeAsync?