title: "ArgumentException: Item has already been added" for hashtable
body: <p>I have a strange behaviour in one of my base methods of an ASP.net web service.
  First of all: this is code which is productive since years, there has not been reported any problems with it so far. But in my unit tests I recognized two times this problem, so I am confused now, if it is only in my development or if it is a real problem.</p>
  
  <p>Here is my code snippet, where the error </p>
  
  <blockquote>
    <p>System.ArgumentException: Item has already been added. Key in dictionary: '6'  Key being added: '6'</p>
  </blockquote>
  
  <p>occured when <code>_messages.Add</code> is called:</p>
  
  <pre><code>public Message Add(GatewayMessageTypes type, string strMessage, CultureInfo language)
  {
      var message = new Message(type, strMessage, language);
  
      int intCount = _messages.Count + 1;
      if (_messages.ContainsKey(intCount))
      {
          _messages.Remove(intCount);
      }
  
      _messages.Add(intCount, message);
  
      return message;
  }
  </code></pre>
  
  <p><code>_messages</code> is defined as:</p>
  
  <pre><code>public class MessageHandler
  {
      private readonly Hashtable _messages = new Hashtable();
  </code></pre>
  
  <p>And this MessageHandler is used in all my Web Services defined in a base class</p>
  
  <pre><code>public abstract class ServiceBase
  {
      public MessageHandler MessageHandler { get; protected set; }
  </code></pre>
  
  <p>This is the StackTrace:</p>
  
  <pre><code>   at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
     at System.Collections.Hashtable.Add(Object key, Object value)
     at GISGatewayCore.MessageHandler.Add(GatewayMessageTypes strType, String strMessage, CultureInfo language) in GISGateway\GISGatewayCore\MessageHandler.cs:line 69
     at GISGatewayCore.MessageHandler.AddAndLog(GatewayMessageTypes type, String strMessage, CultureInfo language) in GISGateway\GISGatewayCore\MessageHandler.cs:line 81
     at GISGateway.Services.GetClosestFacilityServices.&lt;&gt;c__DisplayClass4.&lt;GetClosestFacilities&gt;b__1(Object index) in GISGateway\GISGateway.Services\GetClosestFacilityServices.cs:line 592
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ThreadHelper.ThreadStart(Object obj)
  </code></pre>
  
  <p>It is right, there are already 6 entries in the collection, and number 6 looks exactly like the new message which should be added.
  So what is the problem?</p>
  
  <ol>
  <li>Did the remove fail? Was it to slow in execution? But the code is synchronously, there should not be any problem? </li>
  <li>Two different entries with the same values? I am quite sure that this could be excluded</li>
  <li>Are there two parallel processes using the same code? My unit tests use this part in common, two of my tests uses the same web service endpoint. But none of my objects are static, so how could this happen?</li>
  </ol>