Всем привет. Проект на Asp.Net Web Api, для работы с бд использую EF. Есть класс UnitOfWork:
public class UnitOfWork : IUnitOfWork
{
private static ApplicationDbContext _context = new ApplicationDbContext();
private CategoryRepository _categoryRepository;
private IGenericRepository<Image> _imageRepository;
private IGenericRepository<Post> _postRepository;
private IGenericRepository<PostComment> _postCommentRepository;
private IGenericRepository<Project> _projectRepository;
private IGenericRepository<ProjectComment> _projectCommentRepository;
private TagRepository _tagRepository;
public CategoryRepository CategoryRepository
{
get
{
if (_categoryRepository == null)
{
_categoryRepository = new CategoryRepository(_context);
}
return _categoryRepository;
}
}
public IGenericRepository<Image> ImageRepository
{
get
{
if (_imageRepository == null)
{
_imageRepository = new GenericRepository<Image>(_context);
}
return _imageRepository;
}
}
public IGenericRepository<Post> PostRepository
{
get
{
if (_postRepository == null)
{
_postRepository = new GenericRepository<Post>(_context);
}
return _postRepository;
}
}
public IGenericRepository<PostComment> PostCommentRepository
{
get
{
if (_postCommentRepository == null)
{
_postCommentRepository = new GenericRepository<PostComment>(_context);
}
return _postCommentRepository;
}
}
public IGenericRepository<Project> ProjectRepository
{
get
{
if (_projectRepository == null)
{
_projectRepository = new GenericRepository<Project>(_context);
}
return _projectRepository;
}
}
public IGenericRepository<ProjectComment> ProjectCommentRepository
{
get
{
if (_projectCommentRepository == null)
{
_projectCommentRepository = new GenericRepository<ProjectComment>(_context);
}
return _projectCommentRepository;
}
}
public TagRepository TagRepository
{
get
{
if (_tagRepository == null)
{
_tagRepository = new TagRepository(_context);
}
return _tagRepository;
}
}
public void Save()
{
_context.SaveChanges();
}
private bool _disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
if (disposing)
{
_context.Dispose();
}
}
this._disposed = true;
}
public void Dispose()
{
NLog.LogManager.GetCurrentClassLogger().Info("YEEEESSS I'M DISPOSE");
Dispose(true);
GC.SuppressFinalize(this);
}
}
С помощью kernel я его инжекчю в контроллеры:
kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InSingletonScope();
Первый запрос обрабатывается хорошо. Но после 2 появляется ошибка
System.InvalidOperationException: 'The operation cannot be completed because the DbContext has been disposed.'
Ссылка на проект: GitHub
Буду рад правильной критике.