C# DbContext已在自定义角色提供程序中释放
在我的C# DbContext已在自定义角色提供程序中释放,c#,asp.net-mvc,entity-framework,ninject,C#,Asp.net Mvc,Entity Framework,Ninject,在我的CustomRoleProvider中使用DbContext时,我的DbContext处理有问题 我已将绑定设置为: kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope(); kernel.Bind(typeof(IGenericRepository<>)).To(typeof(GenericRepository<>)).InRequestScope(); kernel.Bin
CustomRoleProvider
中使用DbContext时,我的DbContext处理有问题
我已将绑定设置为:
kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
kernel.Bind(typeof(IGenericRepository<>)).To(typeof(GenericRepository<>)).InRequestScope();
kernel.Bind<MyContext>().ToSelf().InRequestScope();
CustomRoleProvider
实现
public class CustomRoleProvider : RoleProvider
{
private readonly IGenericRepository<User> _userRepository;
public CustomRoleProvider()
{
// using Service Locator (anti pattern)
// cause MVC do not support DI in role providers (yet)
_userRepository = DependencyResolver.Current.GetService<IGenericRepository<User>>();
}
public override bool IsUserInRole(string username, string roleName)
{
var user = _userRepository.AsQueryable().Where(x => x.Username == username && x.Role.Name == roleName); // dbcontext is here disposed...
return user.Any();
}
public override string[] GetRolesForUser(string username)
{
var user = _userRepository.AsQueryable().Where(x => x.Username == username).Select(x => x.Role.Name); // dbcontext is here disposed...
return user.ToArray();
}
// omitted...
}
public class GenericRepository<T> : IGenericRepository<T>
where T : class
{
private readonly MyContext _context;
private readonly DbSet<T> _dbSet;
public GenericRepository(MyContext context)
{
_context = context;
_dbSet = context.Set<T>();
}
public IQueryable<T> AsQueryable()
{
return _dbSet.AsQueryable();
}
}
显然RoleProvider的生命周期就是MVC应用程序的整个生命周期。(不记得我在哪里读过) 因此,必须解决每个方法中的依赖关系:(
公共类CustomRoleProvider:RoleProvider
{
public override bool IsUserInRole(字符串用户名、字符串角色名)
{
var userRepository=DependencyResolver.Current.GetService();
var user=userRepository.AsQueryable()。其中(x=>x.Username==Username&&x.Role.Name==roleName);//dbcontext在这里被释放。。。
返回user.Any();
}
公共重写字符串[]GetRolesForUser(字符串用户名)
{
var userRepository=DependencyResolver.Current.GetService();
var user=userRepository.AsQueryable()。其中(x=>x.Username==Username)。选择(x=>x.Role.Name);//dbcontext在此处释放。。。
返回user.ToArray();
}
//省略。。。
}
我不明白这为什么不起作用
kernel.Bind<MyContext>().ToSelf().WhenInjectedInto<RoleProvider>();
kernel.Bind().ToSelf().whenInjectedTo();
我甚至试着在SingletonScope中添加
您似乎已经删除了任何可以帮助我们确定上下文处理位置的代码!您可以添加这些代码吗?您想添加哪些部分?对我来说,这就是所有运行的代码:)好的,的内容是userinrole
方法的开始。添加。但这很直截了当:)但我们越来越近了。。。如何使用repositoryGet
方法?至于为什么最后一行代码没有帮助,您的角色提供程序只创建了一次,因此没有什么区别。但是,如果在注入RoleProvider时DbContext是一个单例,则不应处理它
public class CustomRoleProvider : RoleProvider
{
private readonly IGenericRepository<User> _userRepository;
public CustomRoleProvider()
{
// using Service Locator (anti pattern)
// cause MVC do not support DI in role providers (yet)
_userRepository = DependencyResolver.Current.GetService<IGenericRepository<User>>();
}
public override bool IsUserInRole(string username, string roleName)
{
var user = _userRepository.AsQueryable().Where(x => x.Username == username && x.Role.Name == roleName); // dbcontext is here disposed...
return user.Any();
}
public override string[] GetRolesForUser(string username)
{
var user = _userRepository.AsQueryable().Where(x => x.Username == username).Select(x => x.Role.Name); // dbcontext is here disposed...
return user.ToArray();
}
// omitted...
}
public class GenericRepository<T> : IGenericRepository<T>
where T : class
{
private readonly MyContext _context;
private readonly DbSet<T> _dbSet;
public GenericRepository(MyContext context)
{
_context = context;
_dbSet = context.Set<T>();
}
public IQueryable<T> AsQueryable()
{
return _dbSet.AsQueryable();
}
}
[InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.]
System.Data.Entity.Internal.InternalContext.CheckContextNotDisposed() +34
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +30
System.Data.Entity.Internal.InternalContext.Initialize() +21
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +20
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +79
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +21
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +64
System.Linq.Queryable.Where(IQueryable`1 source, Expression`1 predicate) +81
Presentation.Web.Providers.CustomRoleProvider.GetRolesForUser(String username) in ~\Application\Presentation.Web\Providers\CustomRoleProvider.cs:38
System.Web.Security.RolePrincipal.IsInRole(String role) +183
Presentation.Web.Controllers.BaseController.OnActionExecuting(ActionExecutingContext filterContext) in ~\Application\Presentation.Web\Controllers\BaseController.cs:27
...
public class CustomRoleProvider : RoleProvider
{
public override bool IsUserInRole(string username, string roleName)
{
var userRepository = DependencyResolver.Current.GetService<IGenericRepository<User>>();
var user = userRepository .AsQueryable().Where(x => x.Username == username && x.Role.Name == roleName); // dbcontext is here disposed...
return user.Any();
}
public override string[] GetRolesForUser(string username)
{
var userRepository = DependencyResolver.Current.GetService<IGenericRepository<User>>();
var user = userRepository .AsQueryable().Where(x => x.Username == username).Select(x => x.Role.Name); // dbcontext is here disposed...
return user.ToArray();
}
// omitted...
}
kernel.Bind<MyContext>().ToSelf().WhenInjectedInto<RoleProvider>();