title: NullReferenceExceeption when making async call, but not when making sync call
body: <p>Blazor server-side app, .NET5, EF Core 5.0.7</p>
  <p>I have a Blazor component that injects an application database context, and then tries to load some data when the component initialises. This code is in a <code>.razor.cs</code> partial class file...</p>
  <pre class="lang-cs prettyprint-override"><code>    [Inject]
      private ApplicationDbContext Context { get; set; }
  
      private List&lt;User&gt; _feeEarners = new();
  
      protected override async Task OnInitializedAsync() =&gt;
        _feeEarners = await Context.Users.ToListAsync();
  </code></pre>
  <p>I've done this loads and loads of times, and it's always worked fine, but for some reason, the line of code above throws a NRE.</p>
  <p>I've put a breakpoint inside <code>OnInitializedAsync</code> and checked in the debugger, and <code>Context</code> is not null, and I can expand the <code>Users</code> property and see the results, so I can't see what could be null here.</p>
  <p>If I change that line of code so it's not async...</p>
  <pre><code>_feeEarners = Context.Users.ToList();
  </code></pre>
  <p>...then it works fine.</p>
  <p>Anyone any idea what's going on here? As I said, I have done this loads of times, and it's always worked before.</p>
  <p>Thanks</p>
  <p>P.S. As often happens with Blazor, the stack trace is extremely unhelpful...</p>
  <pre><code>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+&lt;&gt;c__11&lt;TResult&gt;+&lt;&lt;InvokeAsync&gt;b__11_0&gt;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.&lt;RunAsync&gt;g__Awaited|0_0(Task task, TagHelperExecutionContext executionContext, int i, int count)
  MySite.Web.Areas.General.Pages.Areas_General_Pages__Host.&lt;ExecuteAsync&gt;b__14_1() in _Host.cshtml
  +
      &lt;component type=&quot;typeof(App)&quot; render-mode=&quot;ServerPrerendered&quot; /&gt;
  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&lt;int&gt; statusCode)
  Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable&lt;int&gt; statusCode)
  Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeNextResultFilterAsync&gt;g__Awaited|29_0&lt;TFilter, TFilterAsync&gt;(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&lt;TFilter, TFilterAsync&gt;(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
  Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
  Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeNextResourceFilter&gt;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.&lt;InvokeAsync&gt;g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
  Microsoft.AspNetCore.Routing.EndpointMiddleware.&lt;Invoke&gt;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)
  </code></pre>