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

Oyb*_*bek 9 .net c# asp.net-mvc solrnet

我有问题.本地一切正常,但在生产服务器中,它总是抛出异常'响应在此上下文中不可用'.可能是什么问题?我注意到由于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;
    }
Run Code Online (Sandbox Code Playgroud)

我根本不使用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`
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 9

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

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

这是一篇博客文章,其中包含了与HttpRequest相似的情况.


All*_*ice 5

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

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

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

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

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

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