title: Random NullReferenceException on Asp.Net Core 2.1.1 View
body: <p>I have an Asp.Net Core web site with a view that is bound to a simple view model.  <strong>.NET Framework 4.6.1 Microsoft.AspNetCore 2.1.1</strong></p>
  
  <p>For the majority of requests it works as expected.  <strong>Randomly (approximately 1-5 out of 100 requests) it results in a NullReferenceException</strong>.</p>
  
  <p>If I understand the stack trace correctly it is happening at <strong>Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)</strong> or <strong>Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest.get_Query()</strong></p>
  
  <p>The site uses Authentication.  </p>
  
  <p>I suspect this happened when upgrading to 2.1.1 (from the previous minor version) but can't be sure.</p>
  
  <p>Here are some links which <em>may</em> be related:</p>
  
  <ul>
  <li><a href="https://github.com/aspnet/Home/issues/2799" rel="nofollow noreferrer">https://github.com/aspnet/Home/issues/2799</a></li>
  <li><a href="https://github.com/aspnet/Home/issues/2806" rel="nofollow noreferrer">https://github.com/aspnet/Home/issues/2806</a></li>
  <li><a href="https://github.com/aspnet/Home/issues/3219" rel="nofollow noreferrer">https://github.com/aspnet/Home/issues/3219</a></li>
  </ul>
  
  <p>This is the code:</p>
  
  <p>(note that I can comment out <code>model = _lessonService.GetData(category);</code> - the View still works - and I still randomly get the error - therefore don't believe it's related to the service call)</p>
  
  <p>(note that I can also comment out all the Antiforgery code and still reproduce the problem)</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Lessons(string category)
  {
      LessonViewModel model = new LessonViewModel();
  
      try
      {
          model = _lessonService.GetData(category);
      }
      catch (Exception ex)
      {
          ModelState.AddModelError("", ex.Message );
      }
  
      return View(model);
  }
  </code></pre>
  
  <p><strong>View:</strong></p>
  
  <pre><code>@model ViewModels.LessonViewModel
  @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
  @functions{
      public string GetAntiXsrfRequestToken()
      {
          return Xsrf.GetAndStoreTokens(Context).RequestToken;
      }
  }
  
  &lt;div role="main" class="container"&gt;
  
      &lt;div asp-validation-summary="ModelOnly" class="alert alert-danger" role="alert"&gt;&lt;/div&gt;
  
      &lt;h1&gt;@Model.Title&lt;/h1&gt;
  
  &lt;/div&gt;
  
  &lt;script&gt;
      var token = '@GetAntiXsrfRequestToken()';
  &lt;/script&gt;
  &lt;script src="~/js/site.js" async&gt;&lt;/script&gt;
  </code></pre>
  
  <p><strong>Exception:</strong></p>
  
  <pre><code>System.NullReferenceException:
     at Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest.get_Query (Microsoft.AspNetCore.Http, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at Microsoft.AspNetCore.Mvc.ModelBinding.QueryStringValueProviderFactory.CreateValueProviderAsync (Microsoft.AspNetCore.Mvc.Core, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider+&lt;CreateAsync&gt;d__3.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
     at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider+&lt;CreateAsync&gt;d__2.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
     at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider+&lt;&gt;c__DisplayClass0_0+&lt;&lt;CreateBinderDelegate&gt;g__Bind|0&gt;d.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
     at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+&lt;InvokeInnerFilterAsync&gt;d__13.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+&lt;InvokeNextResourceFilter&gt;d__23.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow (Microsoft.AspNetCore.Mvc.Core, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next (Microsoft.AspNetCore.Mvc.Core, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+&lt;InvokeFilterPipelineAsync&gt;d__18.MoveNext (Microsoft.AspNetCore.Mvc.Core, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at System.Runtime.ExceptionServices.ExceptioAn unhandled exception occurred while processing the request.
  NullReferenceException: Object reference not set to an instance of an object.
  Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest.get_Query()
  
  Stack Query Cookies Headers
  NullReferenceException: Object reference not set to an instance of an object.
  Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest.get_Query()
  Microsoft.AspNetCore.Mvc.ModelBinding.QueryStringValueProviderFactory.CreateValueProviderAsync(ValueProviderFactoryContext context)
  Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.CreateAsync(ActionContext actionContext, IList&lt;IValueProviderFactory&gt; factories)
  Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.CreateAsync(ControllerContext controllerContext)
  Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider+&lt;&gt;c__DisplayClass0_0+&lt;&lt;CreateBinderDelegate&gt;g__Bind|0&gt;d.MoveNext()
  Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
  Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
  Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
  Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
  Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
  Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
  Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
  Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
  
  Show raw exception details
  System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest.get_Query()
     at Microsoft.AspNetCore.Mvc.ModelBinding.QueryStringValueProviderFactory.CreateValueProviderAsync(ValueProviderFactoryContext context)
     at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.&lt;CreateAsync&gt;d__3.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Mvc.ModelBinding.CompositeValueProvider.&lt;CreateAsync&gt;d__2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.&lt;&gt;c__DisplayClass0_0.&lt;&lt;CreateBinderDelegate&gt;g__Bind|0&gt;d.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.&lt;InvokeInnerFilterAsync&gt;d__13.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.&lt;InvokeNextResourceFilter&gt;d__23.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.&lt;InvokeFilterPipelineAsync&gt;d__18.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.&lt;InvokeAsync&gt;d__16.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Builder.RouterMiddleware.&lt;Invoke&gt;d__4.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Session.SessionMiddleware.&lt;Invoke&gt;d__9.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Microsoft.AspNetCore.Session.SessionMiddleware.&lt;Invoke&gt;d__9.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.&lt;Invoke&gt;d__7.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.&lt;Invoke&gt;d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.&lt;Invoke&gt;d__7.MoveNext()
  </code></pre>