title: Static readonly initialized variable threw null reference exception?
body: <p><strong>Stack Trace</strong></p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
     at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
     at Mobile.DataStructures.Caching.Cache`2.Add(TKey key, TValue value, Func`1 autoRefresh, TimeSpan timeToExpire) in C:\Project\Caching\Cache.cs:line 68
     at Mobile.DataStructures.Caching.Cache`2.Add(TKey key, TValue value, Func`1 autoRefresh) in C:\Project\Caching\Cache.cs:line 51
     at Mobile.Authentication.Principal.PrincipalExtensions.AddUserPermissionsToCache(String username) in C:\Project\Helpers\Auth\PrincipalExtensions.cs:line 53
     at Mobile.Authentication.Principal.PrincipalExtensions.RetrieveUserPermissions(PrincipalWithActivities principal, String username) in C:\Project\Helpers\Auth\PrincipalExtensions.cs:line 37
     at Mobile.MvcApplication.Application_PostAuthenticateRequest(Object sender, EventArgs e) in C:\Project\Global.asax.cs:line 126
  </code></pre>
  
  <p><strong>Cache.cs</strong></p>
  
  <pre><code>public class Cache&lt;TKey, TValue&gt;
  {
      private static readonly IDictionary&lt;TKey, CacheItem&lt;TValue&gt;&gt; _Cache = new Dictionary&lt;TKey, CacheItem&lt;TValue&gt;&gt;();
  
      ...
  
      /// &lt;summary&gt;
      /// Adds the item to the cache where it will use the indicated autoRefresh function to refresh the value when expired.
      /// Will set the time to expire to the indicated time.
      /// &lt;/summary&gt;
      public void Add(TKey key, TValue value, Func&lt;TValue&gt; autoRefresh, TimeSpan timeToExpire)
      {
          var item = new CacheItem&lt;TValue&gt;()
          {
              Value = value,
              RefreshCache = autoRefresh,
              CachedOn = DateTime.Now,
              TimeToExpire = timeToExpire
          };
  
          _Cache.Add(key, item); // NULL-REFERENCE EXCEPTION: This line threw the NullReferenceException.
          TriggerChanged(new CacheEventArgs&lt;TKey, TValue&gt;(key, null, item));
  
          // REFRESH: If the value is already null or default refresh it immediately.
          if (item.Value == null || item.Value.Equals(default(TValue)))
          {
              var before = new CacheItem&lt;TValue&gt;(item);
              item.Value = item.RefreshCache();
              TriggerRefreshed(new CacheEventArgs&lt;TKey, TValue&gt;(key, before, item));
          }
      }
  }
  </code></pre>
  
  <p><strong>Enivornment &amp; Other Details</strong></p>
  
  <p>This is part of an ASP.NET MVC 5 project and the top of the execution chain is the <code>Application_PostAuthenticateRequest(object sender, EventArgs e)</code> event in the <code>global.asax</code> file.</p>
  
  <p>I've only experienced this immediately after deploying to the server; however, I feel like this should be able to happen at all. Shouldn't the <code>static</code> variable be initialized by that point?</p>
  
  <p><strong>Question</strong></p>
  
  <p>It is <code>static</code>, <code>readonly</code>, and it has a static initializer. How did the line <code>_Cache.Add(key, item);</code> throw a <code>NullReferenceException</code>?</p>