2017-06-06 77 views
0

我收到一個不經常發生的錯誤,但客戶端已經看到它並希望它修復。在什麼情況下C#ASP.NET HttpContext.Current.Session拋出IndexOutOfRangeException?

基本上,它是一個IndexOutOfRangeException例外,但有什麼奇怪的是,跟蹤指向System.Web.HttpContext.Current。這怎麼可能?

它未能上的線是這樣的:


處 System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(字符串名稱 System.Collections.ArrayList.Add(對象的值) ,對象值) System.Web.SessionState.SessionStateItemCollection.set_Item(字符串 名稱,對象的值)在 WebStateManager.set_Item(String鍵,對象的值) 在 \ WebStateManager.cs:線 53在UIStateManager.get_BookingParameters()在 WS \ App_Code文件\經理\ UIStateManager.cs:線
指數的 陣列的範圍以外。

System.Web.HttpContext context = System.Web.HttpContext.Current; 

如果它是一個數組,我可以做計數等檢查,但什麼樣的檢查會比我一個try-catch在這裏做其他的? enter link description here

public class Example 
{ 
    public static void Main() 
    { 
     int[] values1 = { 3, 6, 9, 12, 15, 18, 21 }; 
     int[] values2 = new int[6]; 

     // Assign last element of the array to the new array. 
     values2[values1.Length - 1] = values1[values1.Length - 1]; 
    } 
} 
// The example displays the following output: 
//  Unhandled Exception: 
//  System.IndexOutOfRangeException: 
//  Index was outside the bounds of the array. 
//  at Example.Main() 

我的項目代碼

public override object this[string key] 
     { 
      get 
      { 
       if (Helpers.CommonFunctions.IsHttpSessionNull) 
       { return null; } 

       return HttpContext.Current.Session[key + Helpers.CommonFunctions.GetAppDomainMultiplexer()]; 
      } 
      set 
      { 
       if (Helpers.CommonFunctions.IsHttpSessionNull) 
       { return; } 

       if (value == null) 
       { 
        try 
        { 
         if (HttpContext.Current.Session[key + Helpers.CommonFunctions.GetAppDomainMultiplexer()] != null) 
         { 
          HttpContext.Current.Session.Remove(key + Helpers.CommonFunctions.GetAppDomainMultiplexer()); 
         } 
        } 
        catch 
        { 
        } 
       } 
       else 
       { 
         HttpContext.Current.Session[key + Helpers.CommonFunctions.GetAppDomainMultiplexer()] = value; 

       } 
      } 
     } 

故障堆棧跟蹤如下;

0:072> !clrstack 
    OS Thread Id: 0x31ec (72) 
      Child SP    IP Call Site 
    000000aea766d968 000007f9736a4650 [HelperMethodFrame: 000000aea766d968] 
    000000aea766da50 000007f9674e0e5a System.Collections.ArrayList.Add(System.Object) 
    000000aea766da90 000007f966655292 System.Collections.Specialized.NameObjectCollectionBase.BaseAdd(System.String, System.Object) 
    000000aea766dae0 000007f9650ac4c9 System.Web.SessionState.SessionStateItemCollection.set_Item(System.String, System.Object) 
    000000aea766db20 000007f90ed89ce9 UTL.WebStateManager.set_Item(System.String, System.Object) 
    000000aea766dbf0 000007f90f29370c WebStateManagerHelper.get_OriginalPNR() 
    000000aea766dc80 000007f90f29242d QueryDynamicLoggingComponent.LogTransaction(System.String, System.String) 
    000000aea766e110 000007f90f2917e3 WSHelper.Log(System.String, System.String, Boolean, System.String) 
    000000aea766e160 000007f90f28fd17 WSHelper.GetResponse(System.String, SecurityInfo, System.String, System.String, System.String ByRef, System.String, System.String) 
    000000aea766e5d0 000007f90f29eae6 WSHelper.SendQuery(System.String, SecurityInfo, System.String) 
    000000aea766e7f0 000007f90f29e7f8 WSHelper.SendQuery(SecurityInfo, System.String) 
    000000aea766e840 000007f90f29e4af APIWSPool.SendAndReceiveQueryToString(Agency, System.String, Token, Boolean) 
    000000aea766e940 000007f90f29e374 APIWSPool.SendAndReceiveQuery(Agency, Token, Boolean) 
    000000aea766e9b0 000007f90f6168f4 FlightBookingManager.SearchFlightForMPSearchedFlightRecommendations1(Agency, FlightFareDrivenSearchInfo, Boolean) 
    000000aea766eb80 000007f90f615ec1 ApiFlightBookingProvider.SearchFlightForMPSearchedFlightRecommendations1(Agency, FlightFareDrivenSearchInfo, Boolean) 
    000000aea766ebe0 000007f90f6158f2 APICOM.Threading.OWCOutboundSearchThread.Work() 
    000000aea766edb0 000007f9674e2d45 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 
    000000aea766ef10 000007f9674e2ab9 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 
    000000aea766ef40 000007f9674e2a97 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) 
    000000aea766ef90 000007f9674fa161 System.Threading.ThreadHelper.ThreadStart() 
    000000aea766f2a8 000007f96e0eab53 [GCFrame: 000000aea766f2a8] 
    000000aea766f5f8 000007f96e0eab53 [DebuggerU2MCatchHandlerFrame: 000000aea766f5f8] 
    000000aea766f788 000007f96e0eab53 [ContextTransitionFrame: 000000aea766f788] 
    000000aea766f9a8 000007f96e0eab53 [DebuggerU2MCatchHandlerFrame: 000000aea766f9a8] 

回答

0

SessionStateItemCollection是不是線程安全的(見https://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstateitemcollection(v=vs.110).aspx),你可能有多個線程寫入/在同一時間從會話狀態讀數。

您需要找到訪問HttpContext.Session或Page.Session的代碼,並確保代碼不在後臺線程中運行。

https://msdn.microsoft.com/en-us/library/system.indexoutofrangeexception(v=vs.110).aspx - (搜索「違反線程安全」)

下面是一個使用鎖,以儘量避免問題的一些示例代碼。此代碼非常快速和骯髒。我不推薦它。但很少重新設計你的系統,這可能是值得的。如果你使用這種技術,你必須把它放在的每個使用會話。

public override object this[string key] 
    { 
     get 
     { 
      if (Helpers.CommonFunctions.IsHttpSessionNull) 
      { return null; } 

      lock (HttpContext.Current.Session) 
      { 
       return HttpContext.Current.Session[key + Helpers.CommonFunctions.GetAppDomainMultiplexer()]; 
      } 
     } 
     set 
     { 
      if (Helpers.CommonFunctions.IsHttpSessionNull) 
      { return; } 

      lock (HttpContext.Current.Session) 
      { 
       if (value == null) 
       { 
        try 
        { 
         if (HttpContext.Current.Session[key + Helpers.CommonFunctions.GetAppDomainMultiplexer()] != null) 
         { 
          HttpContext.Current.Session.Remove(key + Helpers.CommonFunctions.GetAppDomainMultiplexer()); 
         } 
        } 
        catch 
        { 
        } 
       } 
       else 
       { 
        HttpContext.Current.Session[key + Helpers.CommonFunctions.GetAppDomainMultiplexer()] = value; 

       } 
      } 
     } 
+0

你有任何代碼可以創建任務/線程/後臺工作人員等嗎? – mjwills

+0

你好mjwills, 我知道,這個對象仍然存在(會話狀態)從我們的項目,但它已被刪除的原生asp.net會話管理器(刪除asp.net會話對象)。這個問題不能在本地/現場環境中重現。只發生在生產環境中。 – hodoor

+0

是什麼讓你認爲這是事業@ONURDİKMEN?即使會話過期,你也不應該得到你所看到的例外。該異常的最常見原因是對Session對象的多線程訪問。請參閱https://stackoverflow.com/questions/24987909/is-session-variable-thread-safe-within-a-parallel-for-loop-in-asp-net-page。 – mjwills

相關問題