Whole blazor web app stop working when exception occured

╄→尐↘猪︶ㄣ 提交于 2019-12-13 20:16:29

问题


Please advice me any suitable solution for the following issue,

when the blazor application throws any exception, the whole application goes down and no link is working, until I can run the application through studio again.

what to do with this issue?

thanks & best regards

Edited

(In order to provide requested info)

Steps to reproduce:

  1. Create a blazorserverside app:

  2. Modify IncrementCount

At Counter.razor:

void IncrementCount()
{
    currentCount += 1;
    _ = 0 / (5-currentCount);  // <-- force error when currentCount is 5.
}
  1. Push Click Me button for 5 times to raise error.

  2. Try to navigate to other app pages (Home, Fetch Data) nothing happens because it fails silently on client.

Additional info

On Startup.cs errors are configured:

app.UseExceptionHandler("/errors");

The stack trace errors:

Unhandled exception rendering component: Attempted to divide by zero.
System.DivideByZeroException: Attempted to divide by zero.
   at blaex.Pages.Counter.IncrementCount() in /home/dani/tmp/blaex/Pages/Counter.razor:line 27
   at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[T](MulticastDelegate delegate, T arg)
   at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, Object arg)
   at Microsoft.AspNetCore.Components.Rendering.Renderer.DispatchEventAsync(Int32 eventHandlerId, UIEventArgs eventArgs)

回答1:


Edited Nov'2019

Quoting Detailed errors during Blazor app development:

When your Blazor app isn’t functioning properly during development, it’s important to get detailed error information so that you can troubleshoot and fix the issues. Blazor apps now display a gold bar at the bottom of the screen when an error occurs.

In production, the gold bar notifies the user that something has gone wrong, and recommends the user to refresh the browser.

Also, more info at Handle errors in ASP.NET Core Blazor apps


Intro

I guess this is a very interesting question. Usually we want to match new concepts to all ones. This is happening to me with Blazor architecture, I want to see it like a kind of mvc++. But this is not, a .razor page looks more like a WinForm ( or WPF form ) than a mvc request. When do you have a runtime error on Winforms, all app crash.

Answer

Just like you do on WinForms, you should to handle errors one by one in your Blazor code, like you do on desktop apps, using try catch.

For the errors in Blazor internals, like PreRendering, JS Interop, etc, it seems that Blazor team is still working on it, see Follow Up: Error handling improvements milestones.

Sample 1:

For your code:

void IncrementCount()
{
    currentCount += 1;
    _ = 0 / (5-currentCount);  // <-- force error when currentCount is 5.
}

Solution is:

void IncrementCount()
{
    currentCount += 1;
    try
    {
        _ = 0 / (5-currentCount);
    }
    catch (DivideByZeroException e)
    {
        // handling exception
    }
}

Sample 2:

For DivideByZeroException on .razor page:

<h1> @( (0 / (5-currentCount) ).ToString()  ) </h1>

They are not solution at this time.

Edited work around by Mister Magoo: There is a solution for Sample 2: try..catch - but it's not very practical to do that to all your markup

<h1>
    @try
    {
        @:@((0 / (5 - currentCount)).ToString())
    }
    catch (Exception ex)
    {
        @:@ex.Message;
    }
</h1>


来源:https://stackoverflow.com/questions/56695966/whole-blazor-web-app-stop-working-when-exception-occured

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!