title: Singletons and race conditions
body: <p>Related to this question:</p>
  
  <p><a href="https://stackoverflow.com/questions/23408921/null-reference-in-web-api-call">Null reference in web api call</a></p>
  
  <p>But I'm spawning this off separately because it I think it might really be an issue with Ninject and I don't want to confuse the original question in case I'm wrong about this.</p>
  
  <p>My problem is that I have a web api project that uses Ninject to manage dependency injection and most of the time it works fine. The problem is if you fire off multiple request very quickly right after starting the application, sometimes they fail in some really weird way with <code>NullReferenceExceptions</code> that simply shouldn't exist for objects that don't appear to be null and I'm wondering if it's an issue with Ninject using <code>InSingletonScope</code> and not really giving me a singleton but sometimes giving me something that hasn't finished initializing (but perhaps is initialized by the time I look at it in the debugger). Is that possible? Or am I completely barking up the wrong tree?</p>
  
  <p>I have Ninject set up using Nuget packages and some web resources (sorry I don't seem to have the link to exactly which ones) and I <em>thought</em> I was doing it right. I have references in my project to <code>Ninject</code> and <code>Ninject.Web.Common</code>. In <code>NinjectWebCommon.cs</code> I have this for registering a bunch of services (I've excluded most of them, to isolate just the one that is giving me trouble):</p>
  
  <pre><code>    private static void RegisterServices(IKernel kernel)
      {
          System.Diagnostics.Debug.WriteLine("Registering Services...");
          kernel.Bind&lt;Services.IUserService&gt;().To&lt;Services.UserService&gt;().InSingletonScope();                                 
  
          kernel.Bind&lt;Services.IMyFinderService&gt;().To&lt;Services.MyFinderService&gt;().InSingletonScope();
          //kernel.SingletonBind&lt;Services.IMyFinderService&gt;().To&lt;Services.MyFinderService&gt;();
          //kernel.Bind&lt;Services.IMyFinderService&gt;().ToConstant&lt;Services.MyFinderService&gt;(new Services.MyFinderService(kernel.Get&lt;Services.IUserService&gt;()));
      }  
  </code></pre>
  
  <p>The <code>MyFinderService</code> is the one giving me trouble and note that it's constructor take an <code>IUserService</code> which was registered before it (and may also be the cause of the problem):</p>
  
  <pre><code>    public MyFinderService(IUserService service)
      {
          cache = new Dictionary&lt;Guid, MyClassBase&gt;();
          userService = service;
      }
  </code></pre>
  
  <p>I tried a couple of different approaches for how to bind it (the second one was suggested in an answer to this question: <a href="https://stackoverflow.com/questions/11356864/ninject-insingletonscope-with-web-api-rc">Ninject InSingletonScope with Web Api RC</a>) but it still seems something isn't right when my API controller calls this method on <code>MyFinderService</code>:</p>
  
  <pre><code>public MyClassBase GetThing(Guid id)
  {
      if (cache.ContainsKey(id))
      {
          return cache[id];
      }
      else
      {
          var type = typeof(MyClassBase).Assembly.GetTypes().FirstOrDefault
          (
              t =&gt; t.IsClass &amp;&amp;
                  t.Namespace == typeof(MyClassBase).Namespace + ".Foo" &amp;&amp;
                  t.IsSubclassOf(typeof(MyClassBase)) &amp;&amp;
                  (t.GetCustomAttribute&lt;MyIdAttribute&gt;()).GUID == id
          );
          if (type != null)
          {
              System.Diagnostics.Debug.WriteLine(string.Format("Cache null: {0}",cache == null));
              var param = (MyClassBase)Activator.CreateInstance(type, userService);
              cache[id] = param;
              return param;
          }
          return null;
      }
  }
  </code></pre>
  
  <p>I sometimes get a <code>NullReferenceException</code> on this line:</p>
  
  <pre><code>cache[id] = param;
  </code></pre>
  
  <p>Even though in the debugger <code>cache</code>, <code>id</code> and <code>param</code> all appear to have values.</p>
  
  <p>It was my assumption that when I registered something as a singleton that Ninject would handle all the details of making sure it really is a singleton and locking access during lazy initialization to ensure that it doesn't create a separate one, but maybe I'm wrong about that?</p>
  
  <p>So how can I safely create and register a single instance of my class in the context of a web api project that might get multiple request coming from different threads.</p>