I have recently come across an issue in C#.Net app. The unmodified stack trace looks like below :
2018-09-12 21:08:31,596 [] [112] ERROR PLoggerFactory::RunLogic - Exception : System.Exception: Serialisation errorSystem.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at Block`2.GetConfigFromDB()
at Block`2.GetConfigFromDB()
at Block`2.Begin(IParcelGettable`1 P, Action`1 f)
at Run.<>c__DisplayClass45_0.<RunInNewThread>b__1()
In the above, GetConfigFromDB is called in a stack. But I have verified the code, there is nothing recursive GetConfigFromDB in that. Is this possible?
Please let me know if the code of GetConfigFromDB is required, I will modify it and share.
-----EDIT ------- Code added
private Dictionary<string, object> GetConfigFromDB()
{
blockConfigJson = controller.BlockConfig.GetConfig(this.BlockInstanceId);
if (String.IsNullOrEmpty(blockConfigJson))
{
return new Dictionary<string, object>();
}
Dictionary<string, object> configDictionary = new Dictionary<string, object>();
try
{
configDictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(blockConfigJson);
foreach (var v in configDictionary)
{
var key = "__" + this.BlockInstanceId + "." + v.Key;
if (SharedConfig.ContainsKey(key))
{
SharedConfig[key] = v.Value;
}
else
{
SharedConfig.Add(key, v.Value);
}
if (v.Key.Trim() == "_extraInfo_")
{
dynamic extraInfo = JsonConvert.DeserializeObject(configDictionary["_extraInfo_"].ToString());
JsonConvert.DeserializeObject<List<Variable>>(extraInfo["Variables"].ToString());
Dictionary<string, string> _variablesTemp = new Dictionary<string, string>();
try
{
_variablesTemp = JsonConvert.DeserializeObject<Dictionary<string, string>>(extraInfo["Variables"].ToString());
}
catch (Exception ex)
{
mLogger.Debug("Variable parsing error " + ex);
}
List<Variable> _variables = new List<Variable>();
foreach (KeyValuePair<string, string> kyp in _variablesTemp)
{
_variables.Add(new Variable()
{
variableName = kyp.Key,
variableValue = kyp.Value
});
}
foreach (Variable _variable in _variables)
{
if (!SharedVariables.ContainsKey(_variable.variableName))
{
SharedVariables.Add(_variable.variableName, _variable.variableValue);
new Caching().Send(_variable.variableName, _EvaluateConfigValue(_variable.variableValue, this.blockConfig, this.SharedConfig, this.SharedVariables, this.propagatedConfig, this.propagatedVariables, this.ControlId));
}
}
}
}
}
catch (Exception ex)
{
configDictionary = new Dictionary<string, object>();
throw;
}
return configDictionary;
}
That stack trace shows your method calling Dictionary<,>.Insert(TKey, TValue, bool)
, but of course you never do. You can't, because that is a private method called by Dictionary<,>.Add
, which you do call.
When optimisations are enabled, stack traces are not always 100% accurate. Especially when trivial methods are called, which almost certainly get inlined. Dictionary<,>.Add
is such a trivial method: it literally does nothing else but call Dictionary<,>.Insert
. It looks like enough information was recovered to determine that there was something between Dictionary<,>.Insert
and GetConfigFromDB
, but not what that something might be. Because nothing better is available, the name GetConfigFromDB
is used a second time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.