【问题标题】:Response is not available in this context在这种情况下没有响应
【发布时间】:2011-07-08 12:10:13
【问题描述】:

我有问题。本地一切正常,但在生产服务器中它总是抛出异常'响应在此上下文中不可用'。可能是什么问题?我注意到由于 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<OfferItemPresenter>(Resources.ApplicationResources.SolrServiceURL + "/offer");
        SolrConfigurationManager.InitSolrConnection<SavedQueryItemPresenter>(Resources.ApplicationResources.SolrServiceURL + "/savedquery");
        SolrConfigurationManager.InitSolrConnection<TopProductsPresenter>(Resources.ApplicationResources.SolrServiceURL + "/topproducts");
        SolrConfigurationManager.InitSolrConnection<TopSellersPresenter>(Resources.ApplicationResources.SolrServiceURL + "/topsellers");
        SolrConfigurationManager.InitSolrConnection<MostRequestedItemPresenter>(Resources.ApplicationResources.SolrServiceURL + "/mostrequested");
        SolrConfigurationManager.InitSolrConnection<MostRequestedQuery>(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 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详情:System.Web.HttpException:响应在此上下文中不可用。

来源错误:

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

堆栈跟踪:

[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.<Get>b__0(KeyValuePair`2 input) +89
   SolrNet.Utils.<Select>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`

【问题讨论】:

  • 什么时候出现错误(什么时候做什么)?能把出错的代码显示一下吗?
  • 同意;堆栈跟踪和确切的异常细节在这里似乎很关键
  • 我不能说是什么行导致它,因为我无法调试生活版本。本地版本完美运行!
  • @Oybek,Darin 是正确的,这就是原因,现在您必须追踪引用 HttpContext.Current 的代码。考虑到这一点,对 UrlEncode 的调用是什么样的?它在SolrNet.Impl.SolrConnection.&lt;Get&gt;b__0(KeyValuePair'2 input) 制造

标签: c# .net asp.net-mvc solrnet


【解决方案1】:

'响应在此上下文中不可用'。可能是什么问题?

您在 IIS7 集成应用程序池模式而不是经典模式下运行它。在集成模式下,您无法访问 Application_Start 中的 HttpResponse,任何访问它的尝试都会失败。

这是一个blog post,它涵盖了类似的情况,但使用的是 HttpRequest。

【讨论】:

  • 我应该切换到我不想做的经典模式,还是取消我根本找不到的 Request 的使用?
  • @Oybek,两者都是可能的。由您决定。我可能会消除 Request 的使用。也许您正在调用一些尝试访问请求的外部库。从异常堆栈跟踪来看,似乎是 SolrNet 正在尝试访问它。
  • @Oybek 我们可以帮你找到它,我们只需要多看一点代码。它可能嵌套在相对较深的地方。
  • @Oybek 我没有意识到 SolrNet 是一个库。我将为您查看该代码。你用的是什么版本?
  • @Allen,我使用的是 3.1 版
【解决方案2】:

在大量挖掘和查看 SolrNet 代码之后,他们似乎没有做错任何事情。此外,as Darin pointed out in an indirect manner,HttpUtility.UrlEncode 在没有 HttpContext 的代码(例如控制台应用程序)中应该可以正常工作,而且确实如此。

但是,正如VinayC 在他对达林的回答的评论中指出的那样:

实际上,这似乎是一个错误。从 反射器,实际代码似乎是 "如果 (null != 当前 && null != current.Response && ...)" 其中 current 是当前的 http 上下文。问题 这是 Response getter 抛出一个 异常,而不是返回 null

与其抛出描述性过强的异常(毫无疑问,他们试图提供帮助),不如直接返回 null 并让 null 引用异常发生。在这种情况下,他们只是检查空值,所以无论如何都不会发生异常!如果还没有,我会将其报告为错误。

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

如果你愿意的话,你可能会实现延迟启动机制。比起惩罚网站的第一个访问者,这似乎并不算太​​糟糕。

【讨论】:

  • 谢谢 :) 我会花一些时间研究一个可靠的应用后启动解决方法。它仍然无法访问请求,但它可以解决 .net 的错误
  • 好吧,我想,我会用一个全局布尔值将代码放到 Application_BeginRequest 中。在例程中启动此代码后,我会将值设置为 false,以便后续请求不会触发代码。换句话说,我将在第一个页面请求上启动此代码。
【解决方案3】:

这是discussed about a month ago in the SolrNet mailing list

这是 ASP.NET 4 中的回归,here's a mention of this bug

SolrNet 的未来版本将replace System.Web.HttpUtility.UrlEncode 解决此错误。 (或者如果你真的需要这个,为什么不分叉 source code 并修复它?)

编辑I just fixed this.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-15
    • 2021-10-30
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多