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

前端之家收集整理的这篇文章主要介绍了c# – 响应在此上下文中不可用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有问题.在本地,一切工作正常,但在生产服务器中,它总是抛出异常“响应在此上下文中不可用”.可能是什么问题?我注意到,由于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`

解决方法

经过很多挖掘和查看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

@H_502_39@

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

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

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

原文链接:https://www.f2er.com/csharp/93229.html

猜你在找的C#相关文章