-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal] Allow Dispatching to Blazor Renderer Sync Context #46068
Comments
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
Looks good to me! |
API Review Notes:
API Approved! namespace Microsoft.AspNetCore.Components;
public abstract class ComponentBase : IComponent, IHandleEvent, IHandleAfterRender
{
+ protected Task DispatchExceptionAsync(Exception exception);
}
public readonly struct RenderHandle
{
+ public Task DispatchExceptionAsync(Exception exception);
} |
The question probably indicates that we were not clear about the use case. The point of
No, it needs to be usable by components that don't inherit from
The only existing workaround, which we use internally in a couple of places, is to write some code that causes a component to re-render itself and to rethrow the exception during rendering. That is a nasty hack, so adding |
I was thinking it could rethrow, but I did not realize how fatal these dispatched exceptions are. After reading your explanation about this treating the exceptions as equivalent to exceptions in lifecycle methods and then reading https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/handle-errors?view=aspnetcore-7.0#lifecycle-methods, this API makes more sense to me. This is not something you'd want to do by default when the caller to InvokeAsync may be able to handle the exception gracefully.
Okay. This API is approved as proposed then. This is somethings we said would be easy to go and add back after API review. I've updated the approval comment, and I'll mention this in the email with the meeting notes. |
Background and Motivation
Currently, there is not an easy or clean way to marshal exceptions occurring off the Blazor sync context back onto it. The following API would allow developers to directly dispatch exceptions to the renderer.
Fix for #44920
Related community requests: #44871, #27716
Proposed API
Usage Examples
Alternative Designs
An alternative would be to have a method
Dispatch()
that is used likeInvokeAsync()
but returns void instead of a task. This would require fewer lines of code to use it, but also would not allow the developer to get a result back from their work item or know when the async operation is completed.Risks
None that I can think of. Does not cause a breaking change, nor should cause a performance regression.
The text was updated successfully, but these errors were encountered: