Could someone confirm that the app.UseExceptionHandler()
does not work for server-side blazor?
I have seen several cases where my custom ErrorHandler
does not catch exceptions being thrown by my application. Example code
Startup.cs
:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
{
...
app.UseExceptionHandler(new ExceptionHandlerOptions { ExceptionHandler = ErrorHandler.HandleError });
...
}
ErrorHandler.cs
:
public static async Task HandleError(HttpContext context)
{
var error = context.Features.Get<IExceptionHandlerFeature>()?.Error;
var message = error?.Message ?? "[EXCEPTION NOT FOUND]";
return;
}
An example are when my repository are throwing an exception as such:
The instance of entity type cannot be tracked because another instance with the same key value for {'Id'} is already being tracked
My MVC solution are catching all exceptions and it is using similar ErrorHandling implementations.
Indeed lots of ASP Core middleware scenarios will not work completely in a server-side Blazor application. This is because Blazor works with SignalR and the Blazor Hub.
You will see that when starting a Blazor application there are first a few HTTP requests that will pass the pipeline till the end. (in most cases these are the initial page load and then a negotiation phase). But then comes a request to "/_blazor" and this is the moment that the connection stays open to continue communication through websockets. If you have an exception after this phase it will not enter your exception handler.
You can observe this by creating a small middleware class that is registered through the UseMiddleware extension method on IApplicationBuilder . Such a middleware class requires an Invoke method like this for example:
.....
public class TestMiddleware
{
private readonly RequestDelegate _next;
public TestMiddleware(RequestDelegate next)
{
_next = next
}
public async Task Invoke(HttpContext context)
{
await _next(context);
}
....
If you put a breakpoint in the Invoke, you will notice that you will not step further once the context parameter is for path "/_blazor" .
Here is another link that discusses similar problems, different from the exception handler one but also related to the ASP.NET middleware: https://github.com/aspnet/SignalR/issues/1334
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With