简体   繁体   English

生产中的HttpContext.Current.Request为null

[英]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_StartRequest对象。

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: 最终,您有两种选择:

  1. Switch your AppPool to Classic Mode. 将AppPool切换到经典模式。
  2. Do not access the 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.

相关问题 什么HttpContext.Current.Request [“ CarName”]!= null,它在做什么? - what HttpContext.Current.Request[“CarName”] != null, what it is doing? 在HttpContext.Current.Request中模拟ServerVariables - Mock ServerVariables in the HttpContext.Current.Request HttpContext.Request或HttpContext.Current.Request URL中是否缺少“#”字母? - '#' letter is missing in HttpContext.Request or HttpContext.Current.Request url? HttpContext.Current.Request在外部C#类中导致空对象引用 - HttpContext.Current.Request resulting in null object reference in Outer C# class 我可以使用动态重定向HttpContext.Current.Request吗? - can I use dynamic to redirect HttpContext.Current.Request? HttpControllerContext.Request和HttpContext.Current.Request之间的区别 - Difference between HttpControllerContext.Request and HttpContext.Current.Request HttpContext.Current.Request和Page.Request中的Url.Host - Url.Host in HttpContext.Current.Request and Page.Request 带有Angular UI路由器的HttpContext.Current.Request - HttpContext.Current.Request with Angular ui-router 我想在我的Windows窗体中包含HttpContext.Current.Request - I want to include HttpContext.Current.Request in my windows forms HttpContext.Current.Items []和HttpContext.Current.Request []之间有什么区别? - What's the differences between HttpContext.Current.Items[] and HttpContext.Current.Request[]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM