aspnetcore: ASP.NET Core 3.0: NullReferenceException in HttpContext.RequestAborted
We have a middleware that accepts WebSocket requests and receives messages while (!context.RequestAborted.IsCancellationRequested && (socket.State == WebSocketState.Open || socket.State == WebSocketState.Connecting)). After migrating from 2.2 to 3.0 we’re getting the exception below.
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Http.DefaultHttpContext.get_RequestAborted()
at Pasaj.MessagingMiddleware.Receive(WebSocket socket, HttpContext context, String sessionId, Int32 clientVersion) in C:\Web\Patogh\Patogh\Hubs\MessagingMiddleware.cs:line 103
at Pasaj.MessagingMiddleware.InvokeAsync(HttpContext context) in C:\Web\Patogh\Patogh\Hubs\MessagingMiddleware.cs:line 96
at Pasaj.MessagingMiddleware.InvokeAsync(HttpContext context) in C:\Web\Patogh\Patogh\Hubs\MessagingMiddleware.cs:line 96
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Pasaj.HttpExceptionMiddleware.Invoke(HttpContext context) in C:\Web\Patogh\Patogh\Helpers\HttpExceptionMiddleware.cs:line 31
at CoreProfiler.Web.CoreProfilerMiddleware.Invoke(HttpContext context)
at AspNetCoreRateLimit.RateLimitMiddleware`1.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
This doesn’t happen every time, maybe 1 in 10,000. But since we’re dealing with hundreds of messages a second, we get this exception every other minute or so.
Further technical details
- ASP.NET Core 3.0
- IIS on Windows Server 2016
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 16 (10 by maintainers)
Awesome, I’ll close this out.
@davidfowl Thank you for following up on this issue. But we haven’t had this problem since upgrading to .NET 5.0 and we’re currently using 6.0.
You don’t need to look at
HttpContext.RequestAbortedat all but it shouldn’t null ref here, that seems like a bug on our part. I would follow @Tratcher 's advice here and not pass the HttpContext to multiple threads. Get the data you need from the HttpContext before starting the websocket loop.PS: This code has lots of other bugs and issues that should be fixed. I can help with that but I don’t want to deter from the original issue.