Request is not available in this context
Please see IIS7 Integrated mode: Request is not available in this context exception in Application_Start:
The “Request is not available in this context” exception is one of the more common errors you may receive on when moving ASP.NET applications to Integrated mode on IIS 7.0. This exception happens in your implementation of the Application_Start method in the global.asax file if you attempt to access the HttpContext of the request that started the application.
When you have custom logging logic, it is rather annoying to be forced either not to log application_start or to have to let an exception occurs in the logger (even if handled).
It appears that rather than testing for Request
availability, you can test for Handler
availability: when there is no Request
, it would be strange to still have a request handler. And testing for Handler
does not raise that dreaded Request is not available in this context
exception.
So you may change your code to:
var currContext = HttpContext.Current;
if (currContext != null && currContext.Handler != null)
Beware, in the context of a http module, Handler
may not be defined though Request
and Response
are defined (I have seen that in BeginRequest event). So if you need request/response logging in a custom http module, my answer may not be suitable.