sendinblue / APIv3-csharp-library

SendinBlue's C# library for API v3
MIT License
58 stars 26 forks source link

Problem when i try to send an email with c# mvc : The key already existed in the dictionary. #49

Closed tamilcantroz closed 1 year ago

tamilcantroz commented 2 years ago

I have my code asp.net MVC c# running in my controller, When i try my code the first time it works, but if i try the same code twice i got the following error : Server Error in '/' Application. The key already existed in the dictionary. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: The key already existed in the dictionary.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentException: The key already existed in the dictionary.] System.Collections.Concurrent.ConcurrentDictionary`2.System.Collections.Generic.IDictionary<TKey,TValue>.Add(TKey key, TValue value) +12597566 MagentaLMarketing.Controllers.User_SendMailsController.ReSendConfirmationEmailUserList(String username, String langName) in C:\FRANCESCO\Project\MagentaLMarketing\MagentaLMarketing\Controllers\User_SendMailsController.cs:155 lambda_method(Closure , ControllerBase , Object[] ) +147

THIS IS MY CODE (where the erro occurs) --> HERE --> Configuration.Default.ApiKey.Add("api-key", "MYKEY");

                    var apiInstance = new TransactionalEmailsApi();

                    string SenderName = "MagentaL Team";
                    string SenderEmail = EmailSender;
                    SendSmtpEmailSender Email = new SendSmtpEmailSender(SenderName, SenderEmail);
                    string ToEmail = EmailTo;
                    string ToName = EmailToName;
                    SendSmtpEmailTo smtpEmailTo = new SendSmtpEmailTo(ToEmail, ToName);
                    List<SendSmtpEmailTo> To = new List<SendSmtpEmailTo>();
                    To.Add(smtpEmailTo);
                    //HtmlContent, TextContent, Subject,
                    var sendSmtpEmail = new SendSmtpEmail(Email); // SendSmtpEmail | Values to send a transactional email
                    sendSmtpEmail.To = To;
                    sendSmtpEmail.HtmlContent = EmailToBody;
                    sendSmtpEmail.Subject = EmailToObject;

                    // Send a transactional email
                    CreateSmtpEmail result = apiInstance.SendTransacEmail(sendSmtpEmail);

                    var apiInstance2 = new ContactsApi();
                    string email = EmailTo;
                    JObject attributes = new JObject();
                    attributes.Add("FIRSTNAME", receiver_firstname);
                    attributes.Add("LASTNAME", receiver_lastname);
                    List<long?> listIds = new List<long?>();
                    listIds.Add(EmailToListIdSendingBlue);
                    bool emailBlacklisted = false;
                    bool smsBlacklisted = false;
                    bool updateEnabled = true;
                    List<string> smtpBlacklistSender = new List<string>();
                    var createContact = new CreateContact(email, attributes, emailBlacklisted, smsBlacklisted, listIds, updateEnabled, smtpBlacklistSender);
                    CreateUpdateContactModel resultcontact = apiInstance2.CreateContact(createContact);

Apparently the Configuration.Default.ApiKey. already exists, how to avoid this? it is possible to set it in the Web.config? or.. it is possible to check if already exists and avoid to replicate? how to fix this issue? thanks

sschwei1 commented 2 years ago

The ApiKey is just saved in an Dictionary. In your case the key is api-key and its value is the actual apiKey (in your case MYKEY), you can check if the key exists and only add if needed like this:

// check if key exists
if(!Configuration.Default.ApiKey.ContainsKey("api-key"))
{
    // if key doesn't exist, add it
    Configuration.Default.ApiKey.Add("api-key", "MYKEY");
}

You can also use

 Configuration.Default.AddApiKey("api-key", "MYKEY");

which will execute following code under the hood

ApiKey["api-key"] = "MYKEY";

The advantage here is, that you will not get an error by adding the same key multiple times, if the key already exists, it will be overwritten (in your case with the same value)

amitsendinblue commented 1 year ago

Hi @tamilcantroz, I hope the above mentioned solution works for you, closing the issue for now.