title: NullReferenceException when adding to Dictionary in Asynchronous context
body: <p>I am having a very strange behaviour as in this picture:</p>
  
  <p><img src="https://i.stack.imgur.com/kGYap.png" alt="enter image description here"></p>
  
  <p>As you can see in the Watch window, everything that can be possibly <code>null</code> are not <code>null</code>.</p>
  
  <p>Here is the full code of the function:</p>
  
  <pre><code>    public LogInTokenCache GetUserIdFromToken(string token)
      {
          LogInTokenCache item = null;
  
          if (this.TokenCache.ContainsKey(token))
          {
              item = this.TokenCache[token];
  
              if (item.ExpirationTime &lt; DateTime.Now)
              {
                  this.TokenCache.Remove(item.Token);
                  return null;
              }
          }
          else
          {
              LogInTokenBusiness tokenBusiness = new LogInTokenBusiness();
  
              var entity = tokenBusiness.FindToken(token);
              if (entity != null &amp;&amp; entity.Token != null)
              {
                  item = new LogInTokenCache()
                  {
                      Token = entity.Token,
                      UserID = entity.UserId,
                      ExpirationTime = entity.ExpirationTime,
                  };
  
                  this.TokenCache.Add(item.Token, item);
              }
          }
  
          return item;
      }
  </code></pre>
  
  <p>I used Find All References feature, and this is the only where I use the Constructor as well as a declaration (I use it for entire Web App):</p>
  
  <pre><code>public class IdentityController : Controller
  {
  
      private static EmailAdpater EmailAdapter = new EmailAdpater();
      private static UserIdentityTokenShortener TokenShortener = new UserIdentityTokenShortener();
      public static LoginTokenManager LoginTokenManager = new LoginTokenManager();
      ...
  </code></pre>
  
  <p>Has anyone encountered this problem? What did I do wrong?</p>
  
  <p><em>EDIT</em>: Added StackTrace and Details
  <em>EDIT2</em>: Edited title so future people can search this topic in case they have the same problem.</p>
  
  <p><img src="https://i.stack.imgur.com/szTSj.png" alt="enter image description here"></p>
  
  <pre><code>   at System.Collections.Generic.Dictionary12.Insert(TKey key, TValue value, Boolean add)
     at System.Collections.Generic.Dictionary12.Add(TKey key, TValue value)
     at MobileDatingAPI.Models.LoginTokenManager.GetUserIdFromToken(String token) in d:\FPT University\Capstone 2\TFSRepo\Projects\MobileDatingAPI\MobileDatingAPI\Models\LoginTokenManager.cs:line 48
     at MobileDatingAPI.Models.Utils.GetUserFromTokenID(Controller controller, String token, BaseApiViewModels model) in d:\FPT University\Capstone 2\TFSRepo\Projects\MobileDatingAPI\MobileDatingAPI\Models\Utils\Utils.cs:line 68
     at MobileDatingAPI.Controllers.CommunityController.UpdateActivity(String token, Nullable11 longitude, Nullable11 latitude) in d:\FPTUniversity\Capstone 2\TFSRepo\Projects\MobileDatingAPI\MobileDatingAPI\Controllers\CommunityController.cs:line 84
     at lambda_method(Closure , ControllerBase , Object[] )
     at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
     at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary12 parameters)
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary12 parameters)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;BeginInvokeSynchronousActionMethod&gt;b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult12.CallEndDelegate(IAsyncResult asyncResult)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
     at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3d()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;&gt;c__DisplayClass46.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3f()
  </code></pre>