2010-01-27 55 views
1

我收到此錯誤: 散列表插入失敗。負載係數太高。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。.net哈希表插入失敗。負載因子過高

異常詳細信息:System.InvalidOperationException:散列表插入失敗。負載係數太高。

在我的代碼中,我查找字典中的一個鍵,如果它不在那裏,我添加它。經過一番研究,我認爲上面的錯誤是因爲我試圖添加相同的密鑰兩次。

static Dictionary<string, string> settings = new Dictionary<string, string>();

if ((!settings.ContainsKey(Key)) || (settings[Key] == null)) 
      settings.Add(Key, AltValue);//Changes by Reliance Consulting 

有沒有一種更安全的方式做到這一點?

的感謝!

+0

是的.Net 3.5是框架 – aron 2010-01-28 14:36:10

回答

3

你沒有說什麼框架,但我假設.NET:

This error is almost always caused by multiple threads modifying the Hashtable at the same time. The fix is to insert locks before modifying the Hashtable, since Hashtable isn't multiple writer threadsafe.

對於.NET 2.0 ASP.NET環境中,有可能會解決你的問題了一個熱修復: FIX: A System.InvalidOperationException exception occurs when you run a Web application that is based in the .NET Framework 2.0 SP2 or in the .NET Framework 3.5 SP1

0

他們現在也有Windows Server 2003 x32/x64的修復程序。