tags:

views:

912

answers:

3

Hi,

Using HttpContext.Current.Request.ServerVariables["SERVER_NAME"] in integrated mode gives an error in IIS7 as per: http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application%5F5F00%5FStart.aspx

Is there a replacement I can use in global.asax code for HttpContext.Current.Request.ServerVariables["SERVER_NAME"]?

This would be similar to using

String strPath = 
HttpContext.Current.Server.MapPath(HttpRuntime.AppDomainAppVirtualPath);

instead of

//String strPath = 
HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ServerVariables["PATH_INFO"]);
A: 

Isn't it the same as Environment.MachineName?

Alex Reitbort
It's not same on localhost.
çağdaş
A: 

same problem here... hopefully there is a solutions, was only able to use HTTPContext in begin_request, and this one is quite called to often :-(

void Application_BeginRequest(Object source, EventArgs e)
        {
            HttpApplication app = (HttpApplication)source;
            HttpContext context = app.Context;                
            server_name = context.Request.ServerVariables["SERVER_NAME"];
csharpnoob
A: 

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.

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