We have a web application in MVC 4 with Entity Framework 6.1 and using unity as DI framework,When we send many request to a page we got these errors:"The connection's current state is open" or somtimes "The underline provider failed to open" I think it should cause by using one instance of UnitOfWork or DbContex by multiple thread,But we check this and didn't find any thing wrong,Here are our codes
///////////////////////////////////////////////////////////////DatabaseFactory///////////////////////////////////////////////////////////////
public class DatabaseFactory : Disposable, IDatabaseFactory
{
private MyDbContext dataContext;
public MyDbContext Get()
{
var context= dataContext ?? (dataContext = new MyDbContext());
return context;
}
protected override void DisposeCore()
{
var dataContextAdapter = dataContext as IObjectContextAdapter;
if (dataContextAdapter != null && dataContextAdapter.ObjectContext.Connection.State == ConnectionState.Open)
dataContextAdapter.ObjectContext.Connection.Close();
if (dataContext != null)
dataContext.Dispose();
}
}
///////////////////////////////////////////////////////////////UnitOfWork///////////////////////////////////////////////////////////////
public class UnitOfWork :Disposable, IUnitOfWork
{
private readonly IDatabaseFactory databaseFactory;
private MyDbContext dataContext;
private LogContext logContext;
public UnitOfWork(IDatabaseFactory databaseFactory)
{
this.databaseFactory = databaseFactory;
}
protected override void DisposeCore()
{
var dataContextAdapter = dataContext as IObjectContextAdapter;
if (dataContextAdapter != null && dataContextAdapter.ObjectContext.Connection.State==ConnectionState.Open)
dataContextAdapter.ObjectContext.Connection.Close();
if (dataContext != null)
dataContext.Dispose();
if (databaseFactory != null)
databaseFactory.Dispose();
}
protected MyDbContext DataContext
{
get { return (dataContext = databaseFactory.Get()); }
}
public bool Commit()
{
try
{
DataContext.SaveChanges();
return true;
}
catch (Exception)
{
return false;
}
}
}
///////////////////////////////////////////////////////////////RepositoryBase///////////////////////////////////////////////////////////////
public abstract class RepositoryBase<T> : Disposable where T : class
{
private MyDbContext dataContext;
private readonly IDbSet<T> dbset;
protected RepositoryBase(IDatabaseFactory databaseFactory)
{
DatabaseFactory = databaseFactory;
dbset = DataContext.Set<T>();
}
protected override void DisposeCore()
{
var dataContextAdapter = dataContext as IObjectContextAdapter;
if (dataContextAdapter != null && dataContextAdapter.ObjectContext.Connection.State == ConnectionState.Open)
dataContextAdapter.ObjectContext.Connection.Close();
if (dataContext != null)
dataContext.Dispose();
}
protected IDatabaseFactory DatabaseFactory
{
get;
private set;
}
protected MyDbContext DataContext
{
get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
}
public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
{
return dbset.Where(where).ToList();
}
}
///////////////////////////////////////////////////////////////UnityRegisterTypes///////////////////////////////////////////////////////////////
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IDatabaseFactory, DatabaseFactory>(new HttpContextLifetimeManager<IDatabaseFactory>())
.RegisterType<IUnitOfWork, UnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>())
.RegisterType<IMyDbContext, MyDbContext>(new HttpContextLifetimeManager<IMyDbContext>())
.RegisterType(typeof(IRepository<>), typeof(RepositoryBase<>))
//Orders
.RegisterType<IOrdersRepository, OrdersRespository>(new HttpContextLifetimeManager<IOrdersRepository>())
.RegisterType<IOrdersService, OrdersService>(new HttpContextLifetimeManager<IOrdersService>())
//Orders
....
}
Any advice and suggestions will be greatly appreciated