Click here to Skip to main content
Click here to Skip to main content

Migrating ASP.NET Applications to IIS 7 Integrated Mode

, 11 Oct 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Introduction There had been much discussion on how to make your application compatible with the IIS7 Integrated mode. To know how to make your

Editorial Note

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ASP.NET Community

United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.
 
The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
Group type: Collaborative Group

428 members


Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid