Exception was thrown when get multiple EWS clients at same time

Hello,
I want to access mailbox in multiple threads and I initial EWS client for each threads. But If I get new client at same time in different threads, an exception will be thrown like:

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 #=zRbDOjPTypUd0NzyNY51gHmcok2of40H6D3C7hHdsrd$1.#=zjszZ8h$Y2Mhl_jpc0w==()
at #=zRbDOjPTypUd0NzyNY51gHmcok2of40H6D3C7hHdsrd$1…ctor(String #=zQps$0J0Nvr0LFLd1oQ==, ICredentials #=z4QviP5E=, WebProxy #=zGW8q8vI=, Boolean #=zvwNbCWjJ_sF0tuUAcKe0mgI=, String #=zWClqOuwyG_BH0qojLyPcS9I=, ServerVersionInfo #=zwOX3ggtga29H)
at #=zbohFJ_lY$Qgzopb_TMSVLmQuoguqOt1ZN5DhY0U=…ctor(String #=zQps$0J0Nvr0LFLd1oQ==, ICredentials #=z4QviP5E=, WebProxy #=zGW8q8vI=, Boolean #=zvwNbCWjJ_sF0tuUAcKe0mgI=, String #=zWClqOuwyG_BH0qojLyPcS9I=, ServerVersionInfo #=zwOX3ggtga29H)
at #=zv3ULmB82nCHPFcH4$3rwTTbEs10Q15rnUbECe7Y=…ctor(String #=zQps$0J0Nvr0LFLd1oQ==, ICredentials #=z4QviP5E=, WebProxy #=zGW8q8vI=, Boolean #=zvwNbCWjJ_sF0tuUAcKe0mgI=, String #=zWClqOuwyG_BH0qojLyPcS9I=, ServerVersionInfo #=zwOX3ggtga29H)
at #=zNbhkujMsm4qMhPxMkqw_s1QDzk$tpA8DDDZ6y$doWe_O…ctor(String #=zQps$0J0Nvr0LFLd1oQ==, ICredentials #=z4QviP5E=, WebProxy #=zGW8q8vI=, Boolean #=zvwNbCWjJ_sF0tuUAcKe0mgI=, String #=zWClqOuwyG_BH0qojLyPcS9I=, ServerVersionInfo #=zwOX3ggtga29H)
at Aspose.Email.Clients.Exchange.WebService.EWSClient.GetEWSClient(String mailboxUri, ICredentials credentials, WebProxy proxy)
at Aspose.Email.Clients.Exchange.WebService.EWSClient.GetEWSClient(String mailboxUri, ICredentials credentials)
It may be a bug?
PS, I’ve tried Aspose Email 20.2 and 21.5, both of them have this issue.

Thanks,

@xieming95at163.com,
Thank you for the query. To investigate the issue on our side, please share and specify the following:

  • code example causing the error
  • description of client/server environment
System.Threading.Tasks.Task t = new System.Threading.Tasks.Task(() => {
                    try
                    {
                        var client2 = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", credential, null);
                        Console.WriteLine("t1s");

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("t1"+ex.Message);
                    }
                });
                System.Threading.Tasks.Task t2 = new System.Threading.Tasks.Task(() => {
                    try
                    {
                        var client2 = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", credential, null);
                        Console.WriteLine("t2s");

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("t2"+ex.Message);
                    }
                });
                System.Threading.Tasks.Task t3 = new System.Threading.Tasks.Task(() => {
                    try
                    {
                        var client2 = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", credential, null);
                        Console.WriteLine("t3s");

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("t3"+ex.Message);
                    }
                });
                t.Start();
                t3.Start();
                t2.Start();

I create three tasks and each of them initial a ews client. Every time one of them will fail.
client is Window 8.1 and I’m using C#
about server side, I’m testing with Exchange Online.
Thanks,

@xieming95at163.com,
You shouldn’t create multiple threads with different EWSClient instances, because a separate connection is created for each request to the server. But your code example shouldn’t throw exceptions anyway. So I added a ticket with ID EMAILNET-40271 in our tracking system. Our development team will investigate this case.

@xieming95at163.com,
Our development team investigated the issue. Unfortunately, the exception “System.ArgumentException: An item with the same key has already been added” hasn’t been reproduced on our side. But another exception has been found and fixed. It also related to the dictionary and reproduced in the described situation. We need to check the issue on your side. The fix will be released with Aspose.Email 21.6.

Hello @Andrey_Potapov
Thank you. May I know the time of Aspose.Email 21.6 release?

@xieming95at163.com,
Aspose.Email 21.6 will be released towards the end of June.

Hello @Andrey_Potapov
Glad to hear that. Thank you.

The issues you have found earlier (filed as EMAILNET-40271) have been fixed in this update.

Hello,
Issue resolved. Thanks for your help!