c# - Dictonary return object having count -1 ArithemeticFlowException -
i facing issue in arithmetic overflow, have posted detail in question [dictonary tolist arithmeticflowexception
however have found reason, when call method
global.sereverconnections.trygetvalue(key, out connections); it throws overflow exception, having count of connections equal -1.
public static idictionary<string, iset<connectionmanager>> sereverconnections = new concurrentdictionary<string, iset<connectionmanager>>(); public static ilist<connectionmanager> getuserconnections(string username) { //key must not null in case return null if send , empty username if (string.isnullorempty(username)) return null; iset<connectionmanager> connections; global.sereverconnections.trygetvalue(username, out connections); //this make copy of //return (connections != null ? connections.tolist() ?? enumerable.empty<connectionmanager>().tolist() : null); //exception occurs in below line, , connections.count==-1 return (connections != null ? connections.tolist() : null); }
global.sereverconnections concurrentdictionary, , thread-safe. adding hashsets - , not thread-safe.
you can't call hashset.tolist() @ same time someone adding items it.
you need use locking around accesses hashset ensure don't have threading issues. or switch using concurrentdictionary instead of hashset (as per https://stackoverflow.com/questions/18922985/concurrent-hashsett-in-net-framework).
Comments
Post a Comment