[英]HttpContext.Current.Request null in production
In my WCF app i have a Global.asax.cs with something like this to retrieve the base URL: 在我的WCF应用程序中,我有一个类似这样的Global.asax.cs来检索基本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. 在我的开发框(localhost)中它工作正常。 But when i push it to production the System.Web.HttpContext.Current.Request
returns null
. 但是,当我将其推送到生产时, System.Web.HttpContext.Current.Request
返回null
。
Anyone know why this might be happening? 任何人都知道为什么会发生这种情况?
This is likely because in development your App Pool is in "Classic" mode (or on IIS 6-). 这可能是因为在开发过程中,您的应用程序池处于“经典”模式(或在IIS 6上)。
In production, your App Pool is set to "Integrated". 在生产中,您的应用程序池设置为“集成”。 Integrated does not allow you to access the Request
object in Application_Start
. 集成不允许您访问Application_Start
的Request
对象。
Classic behavior allows this because the only way to start an ASP.NET application in Classic Mode is with the first request. 经典行为允许这样做,因为在经典模式下启动ASP.NET应用程序的唯一方法是使用第一个请求。 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 . 您可以在IIS的网站上找到有关原因以及如何解决此问题的更多信息。
Ultimately, you have two options: 最终,您有两种选择:
Request
object in Application_Start. 不要在Application_Start中访问Request
对象。 Moving comment into answer: 将评论转移到答案中:
You should not be trying to access current request inside Application_Start
event. 您不应该尝试访问Application_Start
事件中的当前请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.