发生异常时,整个blazor Web应用程序停止工作 [英] Whole blazor web app stop working when exception occured

查看:384
本文介绍了发生异常时,整个blazor Web应用程序停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请为我解决以下问题的任何合适的解决方案,

Please advice me any suitable solution for the following issue,

当blazor应用程序引发任何异常时,整个应用程序将关闭并且没有任何链接可用,直到我可以再次通过studio运行该应用程序为止.

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.

该问题怎么办?

感谢&最好的问候

thanks & best regards

(为了提供所需的信息)

复制步骤:

  1. 创建blazorserverside应用程序:

  1. Create a blazorserverside app:

修改IncrementCount

Modify IncrementCount

Counter.razor:

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

  1. Click Me按钮5次以引发错误.

  1. Push Click Me button for 5 times to raise error.

尝试导航到其他应用程序页面(主页",获取数据"),因为它在客户端上无提示失败,所以没有任何反应.

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

其他信息

Startup.cs上配置了错误:

app.UseExceptionHandler("/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)

推荐答案

2019年11月编辑

引用

当开发者的Blazor应用无法正常运行时,获取详细的错误信息非常重要,这样您就可以对问题进行故障排除和修复.现在,出现错误时,Blazor应用程序会在屏幕底部显示一个金条.

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.

此外,请访问处理ASP.NET Core Blazor应用程序中的错误

我想这是一个非常有趣的问题.通常,我们希望将新概念与所有概念相匹配. Blazor架构在我身上正在发生这种情况,我想将其视为一种mvc ++.但这不是,.razor页面看起来比mvc请求更像WinForm(或WPF形式). Winforms何时出现运行时错误,所有应用程序都会崩溃.

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.

就像您在WinForms上所做的一样,您应该像使用台式机应用程序一样使用try catch来逐一处理Blazor代码中的错误.

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.

对于Blazor内部的错误(例如PreRendering,JS Interop等),Blazor团队似乎仍在工作,请参见

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.

输入您的代码

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

解决方案是:

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

示例2:

对于.razor页上的DivideByZeroException:

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

目前还不能解决问题.

编辑是由 Magento先生解决的:有一个解决方案对于示例2:try..catch-但是对所有标记执行此操作不是很实际

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>

这篇关于发生异常时,整个blazor Web应用程序停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆