tags:

views:

385

answers:

3

I'm running IIS 7 Integrated mode and I'm getting

Request is not available in this context

when I try to access it in a Log4Net related function that is called from Application_Start. This is the line of code I've

if (HttpContext.Current != null && HttpContext.Current.Request != null)

and an exception is being thrown for second comparison.

What else can I check other than checking HttpContext.Current.Request for null??


A similar question is posted @ http://stackoverflow.com/questions/2056398/request-is-not-available-in-this-context-exception-when-runnig-mvc-on-iis7-5

but no relevant answer there either.

+3  A: 

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.

Andrew Hare
More discussion of this situation here: http://stackoverflow.com/questions/1790457/global-asax-get-the-server-name
jball
Thanks. I had seen that link before.It says:"Basically, if you happen to be accessing the request context in Application_Start, you have two choices:1) Change your application code to not use the request context (recommended).2) Move the application to Classic mode (NOT recommended)."Are their no other options? My logging code writes stuff in DB e.g. Application started, if not via a request than those fields should be set to null rather than completely removing my log statement.
Vishal Seth
A: 

This is a duplicate post of this entry

Since there's no Request context in the pipeline during app start anymore, I can't imagine there's any way to guess what server/port the next actual request might come in on. You have to so it on Begin_Session.

Here's what I'm using when not in Classic Mode. The overhead is negligible.

/// <summary>
/// Class is called only on the first request
/// </summary>
private class AppStart
{
    static bool _init = false;
    private static Object _lock = new Object();

    /// <summary>
    /// Does nothing after first request
    /// </summary>
    /// <param name="context"></param>
    public static void Start(HttpContext context)
    {
        if (_init)
        {
            return;
        }
        //create class level lock in case multiple sessions start simultaneously
        lock (_lock)
        {
            if (!_init)
            {
                string server = context.Request.ServerVariables["SERVER_NAME"];
                string port = context.Request.ServerVariables["SERVER_PORT"];
                HttpRuntime.Cache.Insert("basePath", "http://" + server + ":" + port + "/");
            }
        }
    }
}

protected void Session_Start(object sender, EventArgs e)
{
    //initializes Cache on first request
    AppStart.Start(HttpContext.Current);
}
Laramie
A: 
Gergely Herendi