System.ArgumentException: An item with the same key has already been added. Key: medium

System.ArgumentException: An item with the same key has already been added. Key: medium
at System.Collections.Generic.Dictionary2.TryInsert(TKey key, TValue value, InsertionBehavior behavior) at System.Collections.Generic.Dictionary2.Add(TKey key, TValue value)
at .( , Double& )
at .(CellStyle , ExcelFont , , Boolean)
at .(ExcelCell , ExcelFont , , Boolean)
at .(String , , ExcelCell )
at .(String , )
at .( )
at .( )
at ( )
at .(e )
at .( )
at .( )
at ( )
at .(e )
at .( )
at .(String , HtmlLoadOptions , CellStyle )
at GemBox.Spreadsheet.ExcelCell.SetValue(String html, HtmlLoadOptions options)

Image for better visibility

I was calling the
currentCell.SetValue(“Some html data I have here”.Trim(),
new HtmlLoadOptions { InheritCellStyle = true });

What could be the reason for this to happen?

Hi,

I was unable to reproduce this issue.
Please send us a small Visual Studio project that reproduces this so that we can investigate it.

Regards,
Mario

Thanks for the response Mario. It will take quite the amount of time to try making a minimal reproduction since the code is deeply integrated within out project and it only happen occasionally on the background worker thread which is also hard to catch from my side

Though I suspect it is tied to multithreading / object lifecycle somehow
Will notify if I find a reliable way to reproduce it

Hi Mario, actually I managed to reproduce this constantly

        List<Task> TaskList = new List<Task>();
        for (int i = 0; i < 10; i++)
        {
            var task = Task.Run(async () =>
            {
                var file = ExcelFile.Load("D:\\Tempfile.xlsx");
                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        file.Worksheets[0].Cells[i,j].SetValue("<html>Test</html>",new HtmlLoadOptions { InheritCellStyle = true });
                    }
                }
            });
            TaskList.Add(task);
        }
        await Task.WhenAll(TaskList);

I made a simple reproduction on github

Please try again with this bugfix version:
https://www.gemboxsoftware.com/spreadsheet/nightlybuilds/GBS49v1291.zip

Or this NuGet package:
Install-Package GemBox.Spreadsheet -Version 49.0.1291-hotfix

Does this solve your issue?

Thanks Mario. It looks OK
I will move this version to production and observe