Closed tamilcantroz closed 1 year 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)
Hi @tamilcantroz, I hope the above mentioned solution works for you, closing the issue for now.
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");
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