2014-02-05 13 views
3

我們有一個在IIS 7下工作的MVC 3應用程序和應用程序池託管流水線模式爲集成Mvc 3應用程序隨機停止(特別是在回收後)IIS 7.5集成模式

此應用程序突然變得不穩定,這意味着像下面的錯誤。

Log Name:  Application 
Source:  ASP.NET 4.0.30319.0 
Date:   04.02.2014 12:01:16 
Event ID:  1309 
Task Category: Web Event 
Level:   Warning 
Keywords:  Classic 
User:   N/A 
Computer:  
Description: 
Event code: 3005 
Event message: An unhandled exception has occurred. 
Event time: 04.02.2014 12:01:16 
Event time (UTC): 04.02.2014 10:01:16 
Event ID: 9896973154a54e5b88e6f1799922e853 
Event sequence: 6 
Event occurrence: 1 
Event detail code: 0 

Application information: 
    Application domain: /LM/W3SVC/3/ROOT-1-130359816693915362 
    Trust level: Full 
    Application Virtual Path:/
    Application Path: D:\WebSites\ 
    Machine name: DC1VMCIFWEB02 

Process information: 
    Process ID: 4152 
    Process name: w3wp.exe 
    Account name: NT AUTHORITY\SYSTEM 

Exception information: 
    Exception type: NullReferenceException 
    Exception message: Object reference not set to an instance of an object. 
    at System.Web.PipelineModuleStepContainer.GetEventCount(RequestNotification notification, Boolean isPostEvent) 
    at System.Web.HttpApplication.PipelineStepManager.ResumeSteps(Exception error) 
    at System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) 
    at System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) 

此外,這是我們的global.asax.cs代碼,我想知道這個代碼有什麼問題;

public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

      routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
      ); 

     } 

     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 

      RegisterRoutes(RouteTable.Routes); 
      log4net.Config.XmlConfigurator.Configure(); 
     } 

     protected void Application_AcquireRequestState(object sender, EventArgs e) 
     { 
      Response.Cache.SetExpires(DateTime.Now.AddSeconds(0)); 
      Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      Response.Cache.SetValidUntilExpires(true); 


      if (HttpContext.Current.Session != null) 
      { 
       CultureInfo ci = (CultureInfo)this.Session["Culture"]; 
       if (ci == null) 
       { 
        string langName = "tr"; 

        if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0) 
        { 
         langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2); 
        } 
        ci = new CultureInfo(langName); 
        this.Session["Culture"] = ci; 
       } 
       Thread.CurrentThread.CurrentUICulture = ci; 
       Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name); 
      } 
     } 

     protected void Application_Error(object sender, EventArgs e) 
     { 
      Exception ex = Server.GetLastError(); 

      Application[HttpContext.Current.Request.UserHostAddress.ToString()] = ex; 

      Class.LogClass.Error(this.GetType().ToString(), "Application_Error : ", ex); 
     } 

回答

0

關於這條線:

Application[HttpContext.Current.Request.UserHostAddress.ToString()] = ex; 

MSDN

應用程序狀態被存儲在存儲器中的服務器上,因此在應用狀態的大量數據可以填滿服務器內存快速。如果應用程序重新啓動,則應用程序狀態數據將丟失。應用程序狀態不在Web場中的多個服務器之間或Web場中的工作進程之間共享。最後,應用程序狀態是自由線程的,所以存儲在應用程序狀態中的任何數據都必須具有內置的同步支持。有關這些注意事項的更多信息,請參閱ASP.NET應用程序狀態概述和ASP.NET狀態管理建議。

基本上,您存儲的全局變量是UserHostAddress/Exception object地圖的列表。實質上,這是自上次應用程序池回收後訪問網站的每個IP地址的最後一個錯誤。由於這種狀態只在應用程序池回收時才被清除,因此它會像內存泄漏一樣緩慢。應用程序池超時的時間越長,並且用戶在某處發生異常的可能性越大,您將越有可能在內存不足的情況下運行服務器。

目前還不清楚爲什麼要將異常放入持久位置,但如果您的目的是允許當前請求能夠顯示異常,則更好的選擇是將其存儲在HttpContext.Items中,這將僅持續當前請求的生命週期。這將確保應用程序錯誤最終不會運行服務器內存不足。

我不確定這是什麼導致您的應用程序不穩定,但它是一個開始的好地方。

相關問題