This project has moved. For the latest updates, please go here.

IntegerExtension.ToStringLookup not threadsafe?

Feb 15, 2013 at 9:49 AM
We have a service which generates several reports in parallel threads and got the following exception:

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)
at ClosedXML.Excel.XLAddress.GetTrimmedAddress()
at ClosedXML.Excel.XLWorkbook.GenerateWorksheetPartContent( ......

Could you perhaps look into this?
Mar 1, 2013 at 9:12 AM
Edited Mar 1, 2013 at 10:46 AM
We have a similar system and get the same error
May 2, 2013 at 6:03 AM
Same problem here. Does anyone have a solution?
Dec 12, 2013 at 9:47 AM
same error error using parallel.foreach..
The main question is.. is ClosedXML threadsafe? I see a lot of Dictionary instead of ConcurrentDictionary.
Maybe someone from the project team can make a statement..
Feb 2, 2015 at 5:52 PM
I get a similar error:
System.ArgumentException: An item with the same key has already been added.
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at ClosedXML.Excel.XLAddress.GetTrimmedAddress()
at ClosedXML.Excel.XLWorkbook.GenerateWorksheetPartContent(WorksheetPart worksheetPart, XLWorksheet xlWorksheet, SaveContext context)
at ClosedXML.Excel.XLWorkbook.CreateParts(SpreadsheetDocument document)
at ClosedXML.Excel.XLWorkbook.CreatePackage(String filePath)

I'm using ClosedXML in a web service and the error occurs when there are concurrent calls using the library in separate threads. So it sounds like ClosedXML is not threadsafe but I'm hoping that someone can confirm and if it's possible to make changes to make it threadsafe.
Feb 24, 2015 at 5:52 PM
It looks like this is fixed in https://closedxml.codeplex.com/SourceControl/changeset/fdeab2de14a4cc0db3a799e01439bbfb0599dbc8 and that should be included in version 0.76.0 as far as I can tell from the source.
I quick look in the code finds no other obvious spots that wouldn't be thread safe when you work with different documents on each thread. However if you call different methods on the same document from different threads I wouldn't be surprised if you could mess up internal state in that document.
Feb 26, 2015 at 3:54 PM
We'll give it a shot and see if the issue is resolved. Thanks!