我面對的算術溢出的問題,我已經發布了詳細的這個問題[Dictonary to ToList ArithmeticFlowExceptionDictonary返回對象-1 ArithemeticFlowException
但我已經找到了原因,當我調用該方法
Global.SereverConnections.TryGetValue(key, out connections);
它引發溢出異常,連接數等於-1。
public static IDictionary<string, ISet<ConnectionManager>> SereverConnections = new ConcurrentDictionary<string, ISet<ConnectionManager>>();
public static IList<ConnectionManager> GetUserConnections(string username)
{
//Key must not be null in any case return null if someone send and empty username
if (string.IsNullOrEmpty(username))
return null;
ISet<ConnectionManager> connections;
Global.SereverConnections.TryGetValue(username, out connections);
//this will make the copy of the
//return (connections != null ? connections.ToList() ?? Enumerable.Empty<ConnectionManager>().ToList() : null);
//exception occurs in below line, and connections.Count==-1
return (connections != null ? connections.ToList() : null);
}
有沒有在你的代碼表明這是如何發生野生猜測:'Global.SereverConnections'從多個線程訪問,引起了字典,不一致的狀態結束了 –
你可以在字典中當對象的數量等於-1時共享嗎? – bilal
是的,我在多線程環境下使用它,我正在做負載測試@JeroenMostert – bilal