title: Ninject doesn't call Dispose on objects when out of scope
body: <p>I was surprised to find that at least one of my objects created by Ninject is not disposed of at the end of the request, when it has been defined to be InRequestScope</p>
  
  <p>Here's the object I'm trying to dispose:</p>
  
  <p>Interface:</p>
  
  <pre><code>public interface IDataContext : IDisposable
  {
      MessengerEntities context { get; set; }
  }
  </code></pre>
  
  <p>MessengerEntities is Entity Framework's implementation of ObjectContext -- my context object.</p>
  
  <p>Then I create a concrete class like so:</p>
  
  <pre><code>public class DataContext : IDataContext
  {
      private MessengerEntities _context = new MessengerEntities();
      public MessengerEntities context
      {
          get
          {
              return _context;
          }
          set
          {
              _context = value;
          }
      }
      #region IDisposable Members
  
      public void Dispose()
      {
          context.Dispose();
      }
  
      #endregion
  }
  </code></pre>
  
  <p>And then I have a Ninject controller factory like so (this is modeled on the Steve Sanderson MVC 2 book):</p>
  
  <pre><code>public class NinjectControllerFactory : DefaultControllerFactory
  {
      // a Ninject "kernel" is the thing that can supply object instances
      private IKernel kernel = new StandardKernel(new MessengerServices());
  
      // ASP.NET MVC calls this to get the controller for each request
      protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
      {
          if (controllerType == null)
              return null;
          return (IController)kernel.Get(controllerType);
      }
  
      private class MessengerServices : NinjectModule
      {
          public override void Load()
          {
              Bind&lt;IDataContext&gt;().To&lt;DataContext&gt;().InRequestScope();
              Bind&lt;IArchivesRepository&gt;().To&lt;ArchivesRepository&gt;().InRequestScope();
              Bind&lt;IMessagesRepository&gt;().To&lt;MessagesRepository&gt;().InRequestScope();
          }
      }
  }
  </code></pre>
  
  <p>Now, when I put a breakpoint at the call to context.Dispose() in the DataContext object and run the debugger, that code never gets executed.</p>
  
  <p>So, the evidence suggests that Ninject does not dispose of objects when they go out of scope, but simply creates new objects and relies on the garbage collector to get rid of them at a time of its choosing.</p>
  
  <p>My question is: should I be concerned about this?  Because I am -- I would think Ninject would dispose of any object that implements IDisposable.</p>
  
  <p><strong>UPDATE:</strong> I downloaded the Ninject Mvc extensions (for MVC 3) and this is now how I'm doing the MvcApplication and the binding, and it does seem to be disposing of my context object.</p>
  
  <p>In global.asax:</p>
  
  <pre><code>public class MvcApplication : NinjectHttpApplication
  {
      public static void RegisterGlobalFilters(GlobalFilterCollection filters)
      {
          filters.Add(new HandleErrorAttribute());
      }
  
      public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
          routes.MapRoute(
              "Default", // Route name
              "{controller}/{action}/{id}", // URL with parameters
              new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
          );
  
      }
  
      protected override Ninject.IKernel CreateKernel()
      {
          var kernel = new StandardKernel();
          kernel.Load(Assembly.GetExecutingAssembly());
          return kernel;
      }
  
      protected override void OnApplicationStarted()
      {
          base.OnApplicationStarted();
          AreaRegistration.RegisterAllAreas();
          RegisterGlobalFilters(GlobalFilters.Filters);
          RegisterRoutes(RouteTable.Routes);
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class EFBindingModule : NinjectModule
  {
      public override void Load()
      {
          Bind&lt;IDataContext&gt;().To&lt;DataContext&gt;().InRequestScope();
          Bind&lt;IArchivesRepository&gt;().To&lt;ArchivesRepository&gt;().InRequestScope();
          Bind&lt;IMessagesRepository&gt;().To&lt;MessagesRepository&gt;().InRequestScope();
      }
  }
  </code></pre>
  
  <p>Everything else remains the same. </p>