Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Migrating ASP.NET Applications to IIS 7 Integrated Mode

0.00/5 (No votes)
14 May 2011 1  
Introduction There had been much discussion on how to make your application compatible with the IIS7 Integrated mode. To know how to make your

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Introduction

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

Source Error:

 

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,

  1. Either you can move the application to the Classic mode which is not recommended, Or
  2. 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;

Response.Write(HttpRuntime.AppDomainAppVirtualPath);
Response.Write(HttpContext.Current.Request.ApplicationPath);

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".

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here