title: Windsor IoC DbContext
body: <p>I am trying to add Windsor Castle 3.3.0 IoC with my MVC 5 project dbcontext (EF 6) and have had a look at many articles, but I can't figure out how to do it and keep running into the problem of getting the following error:</p>
  
  <blockquote>
    <p>The operation cannot be completed because the DbContext has been disposed.</p>
  </blockquote>
  
  <p>This error only seems to occur once the first page has loaded and then I try to load subsequent pages</p>
  
  <p>This is my code:</p>
  
  <p><strong>Context Interface</strong></p>
  
  <pre><code>public interface IMyContext
  {
      IDbSet&lt;DBProduct&gt; Products { get; set; }
  }
  </code></pre>
  
  <p><strong>Context</strong></p>
  
  <pre><code>public class MyContext : DbContext, IMyContext
  {
      public MyContext ()
          : base("Name=MyDB")
      {
          Database.SetInitializer&lt;MyContext&gt;(null);
          this.Configuration.ProxyCreationEnabled = false;
      }
  
      public IDbSet&lt;DBProduct&gt; Products { get; set; }
  }
  </code></pre>
  
  <p><strong>Usage</strong></p>
  
  <pre><code>public sealed class CatalogueDAL : ICatalogueDAL
  {
      private IMyContext _dbContext;
  
      public CatalogueDAL(IMyContext dbContext)
      {
          _dbContext= dbContext;
      }
  
      public ICollection&lt;DBProduct&gt; GetAllProducts()
      {
          return _dbContext.Products
                  .OrderBy(x =&gt; x.Order)
                  .ThenBy(x =&gt; x.Name)
                  .ToList();
      }
  }
  
  
  public class CatalogueHelper : ICatalogueHelper
  {
      private ICategoryDAL _categoryDAL;
      private IMapper _mapper;
  
      public CatalogueHelper(ICatalogueDAL catalogueDAL)
      {
          _catalogueDAL = catalogueDAL;
          CreateMapper();
      }
  
      public ICollection&lt;Product&gt; GetAllProducts()
      {
          return _mapper.Map&lt;ICollection&lt;DBProduct&gt;, ICollection&lt;Product&gt;&gt;(_catalogueDAL.GetAllProducts());
      }
  
      private void CreateMapper()
      {
          MapperConfiguration config = new MapperConfiguration(cfg =&gt;
          {
              cfg.CreateProductMap();
          });
  
          _mapper = config.CreateMapper();
      }
  }
  
  
  public class LandingPageController : Controller
  {
      private ICatalogueHelper _helper;
  
      public LandingPageController(ICatalogueHelper helper)
      {
          _helper = helper;
      }
  
      public ActionResult AllProducts()
      {
          return View(_helper.GetAllProducts());
      }
  }
  </code></pre>
  
  <p><strong>Castle Initialiser</strong></p>
  
  <pre><code>container.Register(Component.For&lt;IMyContext&gt;().ImplementedBy&lt;MyContext&gt;().LifeStyle.PerWebRequest);
  container.Register(Component.For&lt;ICatalogueDAL&gt;().ImplementedBy&lt;CatalogueDAL&gt;().LifeStyle.PerWebRequest);
  container.Register(Component.For&lt;ICatalogueHelper&gt;().ImplementedBy&lt;CatalogueHelper&gt;().LifeStyle.PerWebRequest);
  </code></pre>
  
  <p>If I remove the IoC and put a <code>using(var context = new MyContext())</code> in the <code>GetAllProducts</code>, it will work fine but that seems to against the point?</p>
  
  <p>How do I change this to use IoC on the dbContext.  </p>
  
  <p>I have tried just using the MyContext (instead of the interface) and registering that with</p>
  
  <pre><code>container.Register(Component.For&lt;MyContext&gt;().ImplementedBy&lt;MyContext&gt;().LifeStyle.PerWebRequest);
  </code></pre>
  
  <p>And this seems to work better, but when I load two pages at the same time, the one that starts second will always get the following error message:</p>
  
  <blockquote>
    <p>There is already an open DataReader associated with this Command which must be closed first</p>
  </blockquote>
  
  <p>Having searched both these errors, I cannot figure out how to apply it to my solution.  Any help would be greatly appreciated</p>