Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext.Current.Request null in production

Tags:

c#

.net

wcf

In my WCF app i have a Global.asax.cs with something like this to retrieve the base URL:

protected void Application_Start(object sender, EventArgs e)
{
    string baseURL = System.Web.HttpContext.Current.Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
}

In my development box (localhost) it works fine. But when i push it to production the System.Web.HttpContext.Current.Request returns null.

Anyone know why this might be happening?

like image 960
capdragon Avatar asked Mar 01 '26 23:03

capdragon


1 Answers

This is likely because in development your App Pool is in "Classic" mode (or on IIS 6-).

In production, your App Pool is set to "Integrated". Integrated does not allow you to access the Request object in Application_Start.

Classic behavior allows this because the only way to start an ASP.NET application in Classic Mode is with the first request. In Integrated mode, the application may start by other means than a request (Such as application warm-up).

You can find more information on why, and how to fix this on IIS's Website.

Ultimately, you have two options:

  1. Switch your AppPool to Classic Mode.
  2. Do not access the Request object in Application_Start.
like image 174
vcsjones Avatar answered Mar 04 '26 13:03

vcsjones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!