Request is not available in this context exception in Global.asax's Application_Start IIS 7 Integrated mode

Recently we started upgrading an old Asp.NET 2.0 application to Framework 4 and IIS 7. Upgrades went OK and the application started working on our test environment hosted on an older Windows 2003 server. Once the application went to production we started seeing "Request is not available in this context".
We knew the application first call is to a licensing service in the Application_Start() event.
Obviously this exception related to IIS7, because the application worked without any issues on the test environment. After some research we found out this issue is related to IIS 7 Integrated mode. The solution was easy, We created a static constructor in global.asax and used a static readonly variable to use in the Application_Start() event.

Here is the code:

static readonly HttpRequest initialRequest;
static Global()
{
    initialRequest = HttpContext.Current.Request;        
    
}
protected void Application_Start()
{
    //Get the IP Adddress of the host to send for the licensing service
    var domainUrl = initialRequest.ServerVariables["LOCAL_ADDR"];
}

Comments (1) -

Thank you,
Saved me from creating static class and initialize the request manually using locks

Add comment