dotnet / dotnet/aspnetcore

QuickGrid throws InvokeAsync exception when RefreshDataAsync() is called

Open
#58,794 2 comments 0 reactions 0 assignees View on GitHub
area-blazor enhancement feature-blazor-quickgrid
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### 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()`](https://github.com/dotnet/aspnetcore/blob/ffabb299562e92a2e735729694a72e117b11493b/src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/QuickGrid.razor.cs#L283C9-L283C24) without wrapping it in `InvokeAsync` 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:

``` csharp
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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.