c# - 进行异步调用时出现 NullReferenceException,但进行同步调用时则不会出现 NullReferenceException

标签 c# entity-framework-core blazor blazor-server-side

Blazor 服务器端应用、.NET5、EF Core 5.0.7

我有一个 Blazor 组件,它注入(inject)应用程序数据库上下文,然后在组件初始化时尝试加载一些数据。此代码位于 .razor.cs部分类文件...

    [Inject]
    private ApplicationDbContext Context { get; set; }

    private List<User> _feeEarners = new();

    protected override async Task OnInitializedAsync() =>
      _feeEarners = await Context.Users.ToListAsync();

我已经完成了很多次这样的操作,而且总是工作得很好,但由于某种原因,上面的代码行抛出了 NRE。

我在OnInitializedAsync内放置了一个断点并在调试器中检查,并且 Context不为空,我可以展开 Users属性并查看结果,因此我看不到这里可能为 null 的内容。

如果我更改该行代码,使其不是异步的...

_feeEarners = Context.Users.ToList();

...然后就可以正常工作了。

有人知道这是怎么回事吗?正如我所说,我已经做过很多次了,而且以前总是有效的。

谢谢

附注正如 Blazor 经常发生的情况一样,堆栈跟踪极其无用...

MySite.Web.Areas.Cases.Pages.Note.BuildRenderTree(RenderTreeBuilder __builder)
Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)
Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
Microsoft.AspNetCore.Components.Rendering.HtmlRenderer.HandleException(Exception exception)
Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessPendingRender()
Microsoft.AspNetCore.Components.RenderTree.Renderer.AddToRenderQueue(int componentId, RenderFragment renderFragment)
Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged()
Microsoft.AspNetCore.Components.ComponentBase.CallOnParametersSetAsync()
Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
Microsoft.AspNetCore.Components.Rendering.HtmlRenderer.HandleException(Exception exception)
Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderRootComponentAsync(int componentId, ParameterView initialParameters)
Microsoft.AspNetCore.Components.Rendering.HtmlRenderer.CreateInitialRenderAsync(Type componentType, ParameterView initialParameters)
Microsoft.AspNetCore.Components.Rendering.HtmlRenderer.RenderComponentAsync(Type componentType, ParameterView initialParameters)
Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext+<>c__11<TResult>+<<InvokeAsync>b__11_0>d.MoveNext()
Microsoft.AspNetCore.Mvc.ViewFeatures.StaticComponentRenderer.PrerenderComponentAsync(ParameterView parameters, HttpContext httpContext, Type componentType)
Microsoft.AspNetCore.Mvc.ViewFeatures.ComponentRenderer.PrerenderedServerComponentAsync(HttpContext context, ServerComponentInvocationSequence invocationId, Type type, ParameterView parametersCollection)
Microsoft.AspNetCore.Mvc.ViewFeatures.ComponentRenderer.RenderComponentAsync(ViewContext viewContext, Type componentType, RenderMode renderMode, object parameters)
Microsoft.AspNetCore.Mvc.TagHelpers.ComponentTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.<RunAsync>g__Awaited|0_0(Task task, TagHelperExecutionContext executionContext, int i, int count)
MySite.Web.Areas.General.Pages.Areas_General_Pages__Host.<ExecuteAsync>b__14_1() in _Host.cshtml
+
    <component type="typeof(App)" render-mode="ServerPrerendered" />
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.SetOutputContentAsync()
MySite.Web.Areas.General.Pages.Areas_General_Pages__Host.ExecuteAsync() in _Host.cshtml
+
  Layout = null;
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

最佳答案

这并不是真正的答案,但没有其他地方可以用代码进行长评论。

主要区别:

protected override async Task OnInitializedAsync() 
   => _feeEarners = await Context.Users.ToListAsync();

protected override Task OnInitializedAsync() 
   {
_feeEarners = Context.Users.ToList();
return Task.CompletedTask;
}

在异步版本中,在实际填充 _feeEarners 之前可能会产生 yield ,因此当 _feeEarners 为空时组件会进行初始渲染。因此,问题很可能出在 UI 代码中 - 我们在问题中没有看到的代码。

这里有相关的ComponentBase代码供引用。调用 OnInitializedAsync 并将返回的任务分配给局部变量。如果它产生,则调用 StateHasChanged 并等待它。渲染器获取线程时间来运行其队列并渲染组件。

        private async Task RunInitAndSetParametersAsync()
        {
           OnInitialized();
            var task = OnInitializedAsync();

            if (task.Status != TaskStatus.RanToCompletion && task.Status != TaskStatus.Canceled)
            {
                // Call state has changed here so that we render after the sync part of OnInitAsync has run
                // and wait for it to finish before we continue. If no async work has been done yet, we want
                // to defer calling StateHasChanged up until the first bit of async code happens or until
                // the end. Additionally, we want to avoid calling StateHasChanged if no
                // async work is to be performed.
                StateHasChanged();

                try
                {
                    await task;
                }
                catch // avoiding exception filters for AOT runtime support
                {
                    // Ignore exceptions from task cancellations.
                    // Awaiting a canceled task may produce either an OperationCanceledException (if produced as a consequence of
                    // CancellationToken.ThrowIfCancellationRequested()) or a TaskCanceledException (produced as a consequence of awaiting Task.FromCanceled).
                    // It's much easier to check the state of the Task (i.e. Task.IsCanceled) rather than catch two distinct exceptions.
                     if (!task.IsCanceled)
                     {
                        throw;
                     }
                }

                // Don't call StateHasChanged here. CallOnParametersSetAsync should handle that for us.
            }

            await CallOnParametersSetAsync();
        }

关于c# - 进行异步调用时出现 NullReferenceException,但进行同步调用时则不会出现 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69843652/

相关文章:

c# - 我必须使用什么驱动程序才能在 Edge Chromium 浏览器上的 c# 中运行 selenium 测试?

c# - 如何在 EF Core 代码首次迁移中进行数据迁移?

entity-framework-core - EF 7/Core 中的 AddOrUpdate 发生了什么?

c# - 在方法内部更改参数值,这是反模式吗?

c# - 在枚举中明确定义标志组合

c# - 将域模型业务实体传递给 UI 层问题

sql-server - EF Core SQL Server 使用 GETDATE() 代替 C# DateTime.Now

blazor - 仅允许特定文件类型在 blazor 中上传

asp.net - <AuthorizeView> 在 Visual Studio 中本地运行但在 IIS 上不运行时有效吗?

iis - 我无法使用在 IIS 上发布的 blazor webassembly 执行网站