There had been much discussion on how to make your application compatible with the IIS7 Integrated mode. To know how to make your application compatible with IIS7 integrated mode, you first need to understand what the integrated mode is and why we want to use it.
IIS7 released with two modes, Classic mode and the Integrated mode. The former is compatible with previous versions of the IIS. While the later is the enhanced one which supports many features.
In this blog I am going to explain about the “Request is not available in this context” exception.
The “Request is not available in this context” exception is one of the more common errors you may receive when moving ASP.NET applications to Integrated mode on IIS 7.0/7.5. This exception happens in your implementation of the Application_Start method in the global.asax file when you attempt to access the HttpContext of the request. It looks something like this:
Server Error in '/' Application.
Request is not available in this context
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Request is not available in this context
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
As ASP.NET applications were always started by the first request to the application; it used to be possible to get to the request context through the static HttpContext.Current field. In the Classic mode the request context is used to available.
But because of the design change in the IIS7 Integrated Pipeline, makes the request context unavailable in the Application_Start event.
So what if you want to access the request context in the Application_Start event,
- Either you can move the application to the Classic mode which is not recommended, Or
- You can change your application accordingly.
If your choice is to change your application, then you first need to remove the references to HttpContext.Current from the Application_Start.
Firstly if you are using the HttpContext.Current.Request for getting the application path you don’t need to user the request, use HttpRuntime.AppDomainAppVirtualPath instead.
See the code below;
Both will return the same thing.
Or if you want to access the first Request information, here is the sample code if you want to access the request information in Application_Start event:
void Application_BeginRequest(object sender, EventArgs e)
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
// Initialise your first request here..
Get the context and then do you first initialization steps.
On next blog we will be discovering more about "Migrating ASP.NET Applications to IIS 7 Integrated Mode".