处理本地存储中的奇怪Blazor Web应用程序崩溃

时间:2019-12-22 16:56:44

标签: blazor

我正在使用服务器端Blazor,但有一个奇怪的错误,原因是我现在不明白...

此方法出现错误:

    async Task UpdateBasketAtLocalStorage(BasketAnimationType animationType)
    {
        try
        {
            await LocalStorage.SetItemAsync("sushikaoru.Basket", Basket);

            AppState.AnimationHandler(animationType);

            StateHasChanged();
        }
        catch (Exception ex)
        {
            try
            {
                await LocalStorage.RemoveItemAsync("sushikaoru.Basket");
            }
            catch (Exception ex1)
            {
                await LocalStorage.ClearAsync();
                My.Log.Error(ex1);
            }
            My.Log.Error(ex);
        }
    } 

下图(https://ibb.co/dpBg0kN)中显示了堆栈跟踪。

上述方法由事件处理引发,并在此处初始化:

    async Task InitBasket()
    {
        BasketState basket = null;

        try
        {
            basket = await LocalStorage.GetItemAsync<BasketState>("sushikaoru.Basket") ?? new BasketState();
        }
        catch (Exception)
        {
            basket = new BasketState();
        }

        Basket = basket;
        StateHasChanged();

        Basket.OnBasketChanged -= new Action<BasketAnimationType>(async (e) => await UpdateBasketAtLocalStorage(e));
        Basket.OnBasketChanged += new Action<BasketAnimationType>(async (e) => await UpdateBasketAtLocalStorage(e));
    }

这是OnAfterRenderAsync,它通过本地存储初始化篮子加载。

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        try
        {
            await InitBasket();
            await JS.InvokeAsync<object>("scrollFunction");

            if (firstRender)
            {
                await CheckOpeningTime();
            }                
        }
        catch (Exception ex)
        {
            My.Log.Error(ex);
        }
    }

我认为整个结构不好,应该对其进行优化,但是我不知道哪一点会给这里带来麻烦。希望获得修复的有用提示!

谢谢!

0 个答案:

没有答案