在这种情况下无法获得响应

我有问题。 本地一切正常,但在生产服务器中它总是抛出exception’响应在此上下文中不可用’。 可能是什么问题? 我注意到由于global.asax的一些变化,很多人都遇到了这个问题。 这是global.asax的代码,与应用程序启动相关的部分。

protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); Application["SystemUser"] = TUser.GetUserByIdentifier("system").UID; InitializeSolrInstances(); SearchIndexer.DoIndex(); StartRatingTimer(); SolrManager.RecalculateMostRequested(); } private static void InitializeSolrInstances() { SolrConfigurationManager.InitSolrConnection(Resources.ApplicationResources.SolrServiceURL + "/offer"); SolrConfigurationManager.InitSolrConnection(Resources.ApplicationResources.SolrServiceURL + "/savedquery"); SolrConfigurationManager.InitSolrConnection(Resources.ApplicationResources.SolrServiceURL + "/topproducts"); SolrConfigurationManager.InitSolrConnection(Resources.ApplicationResources.SolrServiceURL + "/topsellers"); SolrConfigurationManager.InitSolrConnection(Resources.ApplicationResources.SolrServiceURL + "/mostrequested"); SolrConfigurationManager.InitSolrConnection(Resources.ApplicationResources.SolrServiceURL + "/requestedquery"); } private void StartRatingTimer() { _LastRatingRenewedTime = DateTime.Now; DateTime CurrentTime = DateTime.Now; DateTime StartTime = new DateTime(2011, 1, 1); GlobalSettings.ReIndexMainSolrCores(StartTime, CurrentTime); Timer OfferAndUserRatingRenewerTimer = new Timer() { /*Timer interval for 24 hours*/ Interval = 24 * 60 * 60 * 1000, Enabled = true }; OfferAndUserRatingRenewerTimer.Elapsed += new ElapsedEventHandler(OfferAndUserRatingRenewerTimer_Elapsed); } public void OfferAndUserRatingRenewerTimer_Elapsed(Object Sender, ElapsedEventArgs e) { GlobalSettings.ReIndexMainSolrCores(_LastRatingRenewedTime, e.SignalTime); _LastRatingRenewedTime = e.SignalTime; } 

我根本不使用HttpContext的Response或Request属性。 无论是在全球性的asax本身,还是在要调用的方法中。 帮我。

它显示的是什么。 ”’应用程序中的服务器错误。

在这种情况下无法获得响应。

描述:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

exception详细信息:System.Web.HttpException:响应在此上下文中不可用。

来源错误:

在执行当前Web请求期间生成了未处理的exception。 可以使用下面的exception堆栈跟踪来识别有关exception的起源和位置的信息。

堆栈跟踪:

 [HttpException (0x80004005): Response is not available in this context.] System.Web.Util.HttpEncoder.get_Current() +11406684 System.Web.HttpUtility.UrlEncode(String str, Encoding e) +137 SolrNet.Impl.SolrConnection.b__0(KeyValuePair`2 input) +89 SolrNet.Utils.d__1a`2.MoveNext() +612 SolrNet.Utils.Func.Reduce(IEnumerable`1 source, TResult startValue, Accumulator`2 accumulator) +393 SolrNet.Impl.SolrConnection.Get(String relativeUrl, IEnumerable`1 parameters) +908 SolrNet.Impl.SolrQueryExecuter`1.Execute(ISolrQuery q, QueryOptions options) +195 SolrNet.Impl.SolrBasicServer`1.Query(ISolrQuery query, QueryOptions options) +176 SolrNet.Impl.SolrServer`1.Query(ISolrQuery query, QueryOptions options) +176 TebeComSearchEngine.SolrManager.RecalculateMostRequested() in SolrManager.cs:77 TebeCom.MvcApplication.Application_Start() in Global.asax.cs:101 [HttpException (0x80004005): Response is not available in this context.] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +4043621 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +191 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +352 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +407 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +375 [HttpException (0x80004005): Response is not available in this context.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11612256 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4842149` 

经过大量挖掘和查看SolrNet代码后,他们似乎没有做错任何事情。 此外, 正如Darin以间接方式指出的那样 ,HttpUtility.UrlEncode应该在没有HttpContext的代码中正常工作,例如控制台应用程序,它确实可以。

然而,正如VinayC在他对Darin的答案的评论中指出的那样:

实际上,它似乎是一个错误。 从reflection器,实际代码似乎是“if(null!= current && null!= current.Response && …)”,其中current是当前的http上下文。 这里的问题是Response getter抛出exception,而不是返回null

而不是抛出过于描述性的exception(毫无疑问他们试图帮助),他们应该只返回null并让null引用exception发生。 在这种情况下,他们只是检查空值,所以无论如何都不会发生exception! 如果它还没有,我会把它报告为bug。

不幸的是,这对你意味着你几乎别无选择,只能在经典模式下运行。 从技术上讲,您可以将调用TebeComSearchEngine.SolrManager.RecalculateMostRequested()放在您在application_start生成的线程中,并延迟执行直到应用程序完成启动。 据我所知,没有万无一失的方式以编程方式发出应用程序启动结束的信号,这种方法可能有点混乱。

如果你想要它,你可能会实现延迟启动机制。 与惩罚网站的第一位访客相比,它似乎并不太糟糕。

“在这种情况下无法提供答复”。 可能是什么问题?

您在IIS7集成应用程序池模式而不是经典模式下运行此项。 在集成模式下,您无法访问Application_Start的HttpResponse,任何访问它的任何尝试都将被破坏。

这是一篇博客文章 ,其中包含类似的情况,但使用了HttpRequest。

大约一个月前在SolrNet邮件列表中对此进行了讨论 。

这是ASP.NET 4中的一个回归, 这里提到了这个bug 。

未来的SolrNet版本将取代System.Web.HttpUtility.UrlEncode来解决这个bug。 (或者,如果你真的需要这个,为什么不分叉源代码并修复它?)

编辑 : 我刚刚修好了 。