c# – 响应在此上下文中不可用

前端之家收集整理的这篇文章主要介绍了c# – 响应在此上下文中不可用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有问题.在本地,一切工作正常,但在生产服务器中,它总是抛出异常“响应在此上下文中不可用”.可能是什么问题?我注意到,由于global.asax的一些变化,很多人遇到这个问题.这是global.asax的代码,与应用程序启动有关的部分.
  1. protected void Application_Start() {
  2. AreaRegistration.RegisterAllAreas();
  3. RegisterRoutes(RouteTable.Routes);
  4. Application["SystemUser"] = TUser.GetUserByIdentifier("system").UID;
  5. InitializeSolrInstances();
  6. SearchIndexer.DoIndex();
  7. StartRatingTimer();
  8. SolrManager.RecalculateMostRequested();
  9. }
  10.  
  11. private static void InitializeSolrInstances() {
  12. SolrConfigurationManager.InitSolrConnection<OfferItemPresenter>(Resources.ApplicationResources.SolrServiceURL + "/offer");
  13. SolrConfigurationManager.InitSolrConnection<SavedQueryItemPresenter>(Resources.ApplicationResources.SolrServiceURL + "/savedquery");
  14. SolrConfigurationManager.InitSolrConnection<TopProductsPresenter>(Resources.ApplicationResources.SolrServiceURL + "/topproducts");
  15. SolrConfigurationManager.InitSolrConnection<TopSellersPresenter>(Resources.ApplicationResources.SolrServiceURL + "/topsellers");
  16. SolrConfigurationManager.InitSolrConnection<MostRequestedItemPresenter>(Resources.ApplicationResources.SolrServiceURL + "/mostrequested");
  17. SolrConfigurationManager.InitSolrConnection<MostRequestedQuery>(Resources.ApplicationResources.SolrServiceURL + "/requestedquery");
  18. }
  19.  
  20. private void StartRatingTimer() {
  21. _LastRatingRenewedTime = DateTime.Now;
  22. DateTime CurrentTime = DateTime.Now;
  23. DateTime StartTime = new DateTime(2011,1,1);
  24. GlobalSettings.ReIndexMainSolrCores(StartTime,CurrentTime);
  25. Timer OfferAndUserRatingRenewerTimer = new Timer() {
  26. /*Timer interval for 24 hours*/
  27. Interval = 24 * 60 * 60 * 1000,Enabled = true };
  28. OfferAndUserRatingRenewerTimer.Elapsed += new ElapsedEventHandler(OfferAndUserRatingRenewerTimer_Elapsed);
  29. }
  30. public void OfferAndUserRatingRenewerTimer_Elapsed(Object Sender,ElapsedEventArgs e) {
  31. GlobalSettings.ReIndexMainSolrCores(_LastRatingRenewedTime,e.SignalTime);
  32. _LastRatingRenewedTime = e.SignalTime;
  33. }

我根本不使用HttpContext的Response或Request属性.全局asax本身也不在调用方法内.帮我.

显示的.
`
‘/’应用程序中的服务器错误.

响应在此上下文中不可用.

说明:在执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误的更多信息及其在代码中的位置.

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

错误

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

堆栈跟踪:

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

解决方法

经过很多挖掘和查看SolrNet代码,他们似乎并没有做错什么.此外,as Darin pointed out in an indirect manner,HttpUtility.UrlEncode应该在没有HttpContext(如控制台应用程序)的代码中工作正常,而且它也是如此.

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

Actually,it appears to be a bug. From
reflector,actual code appears to be
“if (null != current && null !=
current.Response && …)” where
current is current http context. Issue
here is that Response getter throws an
exception,instead of returning null

而不是扔这个过分描述性的异常(毫无疑问,他们试图有帮助),他们应该刚刚返回null,并引用null引用异常发生.在这种情况下,他们只是检查null,所以异常不会发生!如果还没有,我会将其报告为错误.

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

如果你这么做,那么你可能会实现延迟的启动机制.相对于惩罚网站的第一个访问者来说,似乎并不太糟糕.

猜你在找的C#相关文章