title: Ninject 3 - Does BeginBlock() override InRequestScope in asp.net WebAPI?
body: <p>My asp.net WebApi project comprises of multiple assemblies for Services, Core and Data Access. In an attempt to use Ninject as my DI container in the project, I added Ninject.Web.Common package from NuGet. Then, I Implemented IDependencyResolver as:</p>
  
  <pre><code>public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
  {
      readonly IKernel kernel;
  
      public NinjectDependencyResolver(IKernel kernel) : base(kernel)
      {
          this.kernel = kernel;
      }
  
      public IDependencyScope BeginScope()
      {
          return new NinjectDependencyScope(this.kernel.BeginBlock());
      }
  }
  
  public class NinjectDependencyScope : IDependencyScope
  {
      IResolutionRoot resolver;
  
      public NinjectDependencyScope(IResolutionRoot resolver)
      {
          this.resolver = resolver;
      }
  
      public object GetService(System.Type serviceType)
      {
          if (resolver == null)
              throw new ObjectDisposedException("this", "This scope has been disposed");
  
          var resolved = this.resolver.Get(serviceType);
          return resolved;
      }
  
      public System.Collections.Generic.IEnumerable&lt;object&gt; GetServices(System.Type serviceType)
      {
          if (resolver == null)
              throw new ObjectDisposedException("this", "This scope has been disposed");
  
          return this.resolver.GetAll(serviceType);
      }
  
      public void Dispose()
      {
          IDisposable disposable = resolver as IDisposable;
          if (disposable != null)
              disposable.Dispose();
  
          resolver = null;
      }
  }
  </code></pre>
  
  <p>Here is my Ninject.Web.Common.cs.</p>
  
  <pre><code>public static class NinjectWebCommon 
  {
      private static readonly Bootstrapper bootstrapper = new Bootstrapper();
  
      /// &lt;summary&gt;
      /// Starts the application
      /// &lt;/summary&gt;
      public static void Start() 
      {
          DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
          DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
          bootstrapper.Initialize(CreateKernel);
      }
  
      /// &lt;summary&gt;
      /// Stops the application.
      /// &lt;/summary&gt;
      public static void Stop()
      {
          bootstrapper.ShutDown();
      }
  
      /// &lt;summary&gt;
      /// Creates the kernel that will manage your application.
      /// &lt;/summary&gt;
      /// &lt;returns&gt;The created kernel.&lt;/returns&gt;
      private static IKernel CreateKernel()
      {
          var kernel = new StandardKernel();
          kernel.Bind&lt;Func&lt;IKernel&gt;&gt;().ToMethod(ctx =&gt; () =&gt; new Bootstrapper().Kernel);
          kernel.Bind&lt;IHttpModule&gt;().To&lt;HttpApplicationInitializationHttpModule&gt;();
          RegisterServices(kernel);
  
          GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
          return kernel;
      }
  
      /// &lt;summary&gt;
      /// Load your modules or register your services here!
      /// &lt;/summary&gt;
      /// &lt;param name="kernel"&gt;The kernel.&lt;/param&gt;
      private static void RegisterServices(IKernel kernel)
      {
          kernel.Bind(x =&gt;
              x.FromAssembliesMatching("WebApiTest.DataAccess.dll")
              .SelectAllClasses()
              .BindAllInterfaces()
              .Configure(config =&gt; config.InRequestScope()));
  
          kernel.Bind(x =&gt;
              x.FromAssembliesMatching("WebApiTest.*.dll")
              .SelectAllClasses()
              .BindAllInterfaces()
              .Configure(config =&gt; config.InTransientScope()));
      }        
  }
  </code></pre>
  
  <p><b>My question is about the code in NinjectDependencyResolver -> BeginScope() method:</b> return new NinjectDependencyScope(this.kernel.BeginBlock());</p>
  
  <p>I would like to have my repositories (implemented in WebApiTest.DataAccess.dll) be request  scoped. I came across this <a href="http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/" rel="noreferrer">post</a> from Nate Kohari. I realize the post is old but the description about Activation Blocks makes me wonder if that is still the current implementation.   </p>
  
  <blockquote>
    <p>There’s one final way of handling scope in Ninject2, through activation blocks. Blocks are >a way to override the scope that was declared on a binding, and instead associate the >activated instances with the block itself. ... </p>
  </blockquote>
  
  <p>So,<b>what is the actual scope of my repositories?</b></p>
  
  <p>Also, sounds to me that the use of BeginBlock() is optional but when i remove it, the first call to the Controller succeeds but any subsequent calls throw exception:</p>
  
  <blockquote>
    <p>Ninject component ICache No such component has been registered in the kernel's component container </p>
  </blockquote>
  
  <p>WHY??</p>