aspnetcore: HttpContext.Request.Method is empty
I have an app that runs asp.net core 2.1 on the full .net framework. Recently, while developing locally, I started getting random errors. Upon investigation, it is due to an empty HttpContext.Request.Method.
In the log instead of
Microsoft.AspNetCore.Hosting.Internal.WebHost: Information: Request starting HTTP/1.1 GET http://…
We see
Microsoft.AspNetCore.Hosting.Internal.WebHost: Information: Request starting HTTP/1.1 http://…
Notice there is no verb?!
We inspect this request inside a middleware Invoke method
What could cause this property to be empty?
I thought I would try to see the raw request and see the HTTP verb, but I couldn’t find a way of seeing the raw request.
If the request really has no verb, I would imagine that asp.net core would throw an exception much earlier?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 18 (7 by maintainers)
Doesn’t matter if you write or read, you are accessing it outside of the request it’ll cause this issue. Unfortunately, today the HttpContext is never null so that check won’t work. 2.1 introduced an optimization that made this happen but it’s basislly danger territory. We’ve made a couple of changes in the next version that will hopefully make the HttpContext property null when accessed outside of the request. On top of that in 3.0 the HttpContext will throw an exception when accessed outside of the requests.
For your logggr scenario the best way to go about doing what you want is with a logging scope that copies all of the appropriate properties, that way there’s no chance of using the HttpContext outside of the request scope.