title: Autofac registration and disposal issues
body: <p>I'm using entity framework 6 and Autofac in my web application.</p>
  
  <p>I inject unit of work with DbContext inside, both externally owned so I can dispose them myself.</p>
  
  <p>DbContext  registered PerLifetimeScope, </p>
  
  <p>Unit of work is a factory, therefore registered as per dependency.</p>
  
  <p>When Executing the first http Get action everthing works fine and I see the unit of work with the context are disposed after the response is coming from the db which is great. </p>
  
  <p>My issue is that whenever I execute a second request, the context for some reason is disposed before I return an IQueryable. Therefore I get an execption saying: </p>
  
  <blockquote>
    <p>The operation could not be executed because the DbContext is disposed.</p>
  </blockquote>
  
  <p>For example - calling the GetFolders method works the first time, and afterwards fails..</p>
  
  <p>I see the context is disposed too early, what I don't understand is what triggers it too soon in the second request..</p>
  
  <pre><code>public interface IUnitOfWork : IDisposable
  {
      bool Commit();
  }
  
  public EFUnitOfWork : IUnitOfWork
  {
      public IRepository&lt;Folder&gt; FoldersRepository {get; set;}
  
      public IRepository&lt;Letter&gt; LettersRepository {get; set;}
  
      private readonly DbContext _context;
  
  
      public EFUnitOfWork(DbContext context, IRepository&lt;Folder&gt; foldersRepo, IRepository&lt;Letter&gt; lettersRepo)
      {
          _context = context;
          _foldersRepo = foldersRepo;
          LettersRepository = lettersRepo;
      }
  
      private bool disposed = false;
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      protected virtual void Dispose(bool disposing)
      {
          if (!disposed)
          {
              if (disposing)
              {
                  _context.Dispose();
              }
  
              disposed = true;
          }
      }
  
      public bool Commit()
      {
          try
          {
              return SaveChanges() &gt; 0;
          }
          catch (DbEntityValidationException exc)
          {
              // just to ease debugging
              foreach (var error in exc.EntityValidationErrors)
              {
                  foreach (var errorMsg in error.ValidationErrors)
                  {
                      logger.Log(LogLevel.Error, "Error trying to save EF changes - " + errorMsg.ErrorMessage);
                  }
              }
  
              return false;
              throw exc;
          }
      }   
  }
  
  
  
  public class Repository&lt;T&gt; : IRepository&lt;T&gt;
  {
      protected readonly DbContext Context;
      protected readonly DbSet&lt;T&gt; DbSet;
  
      public EFRepository(DbContext context)
      {
          Context = context;
      }
  
      public IQueryable&lt;T&gt; Get()
      {
          return DbSet;
      }
  
      public void Add(T item)
      {
          DbSet.Add(item);
      }
  
      public virtual Remove(T item)
      {
          DbSet.Remove(item);
      }
  
      public void Update(T item)
      {
          Context.Entry(item).State = EntityState.Modified;
      }
  
      public T FindById(int id)
      {
          return DbSet.Find(id); 
      }
  }
  
  public class DataService : IDataService
  {
      private Func&lt;IUnitOfWork&gt; _unitOfWorkFactory;
  
      public (Func&lt;IUnitOfWork&gt; unitOfWorkFactory)
      {
          _unitOfWorkFactory = unitOfWorkFactory;             
      }
  
      public List&lt;FolderPreview&gt; GetFolders()
      {
          using(unitOfWork = _unitOfWorkFactory())
          {
              var foldersRepository = unitOfWork.FoldersRepository;
  
              var foldersData = foldersRepository.Get().Select(p =&gt; new FolderPreview
                                  {
                                      Id = p.Id,
                                      Name = p.Name
                                  }).ToList();
  
              return foldersData;
          }
      }
  }
  
  public class FolderPreview
  {
      public int Id {get; set;}
  
      public string Name {get; set;}
  }
  
  
  Startup code:
  
  {
      _container.RegisterGeneric&lt;IRepository&lt;&gt;,Repository&lt;&gt;&gt;().InstancePerLifetimeScope();
      _container.RegisterType&lt;IDataService, DataService&gt;().SingleInstance();
      _container.RegisterType&lt;EFUnitOfWork, IUnitOfWork&gt;().PerDepnendecny().ExternallyOwned();
      _container.RegisterType&lt;DbContext, MyDbContext&gt;().InstancePerLifetimeScope().ExternallyOwned(); 
  }
  </code></pre>
  
  <p>Is this related to singletons some how? Almost all of my application is singletons, the DataService is also Singleton. Anyone?</p>
  
  <p>Thanks!</p>