Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Created Unassigned: IntegerExtension.ToStringLookup not threadsafe [9118]

$
0
0
Hi,
We are using this library in our service which generates several reports in parallel threads and we're getting the following exceptions:

System.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 ClosedXML.Excel.IntegerExtensions.ToStringLookup(Int32 value)

This error is caused by those lines of code (of static class ClosedXML.Excel.IntegerExtensions):
private static readonly Dictionary<int, string> intToString = new Dictionary<int, string>();

public static string ToStringLookup(this int value)
{
if (!IntegerExtensions.intToString.ContainsKey(value))
IntegerExtensions.intToString.Add(value, value.ToString((IFormatProvider) IntegerExtensions.nfi));
return IntegerExtensions.intToString[value];
}

I think replace this dictionary by ConcurrentDictionary and use AddOrUpdate method will do the trick :)

Regards,
Maciej.

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>