Response is not available in this context

Oybek picture Oybek · Jul 8, 2011 · Viewed 19.8k times · Source

I have problem. Locally everything works fine but in the production server it always throws exception 'Response is not available in this context'. What can be the problem? I've noticed that a lot of people experience this problem due to some changes of global.asax. Here is the code of global.asax, the part related to application start.

    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;
    }

I do not use Response or Request properties of HttpContext at all. Neither in global asax itself, nor within the methods to be called. Help me.

That what it shows. ` Server Error in '/' Application.

Response is not available in this context.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Response is not available in this context.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[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`

Answer

Darin Dimitrov picture Darin Dimitrov · Jul 8, 2011

'Response is not available in this context'. What can be the problem?

You are running this in IIS7 Integrated Application Pool mode instead of Classic mode. In Integrated mode you don't have access to the HttpResponse in Application_Start any any attempt to access it will blow.

Here's a blog post which covers a similar situation but with the HttpRequest.