DbContext已在自定义RoleProvider中释放

本文关键字:释放 RoleProvider 自定义 DbContext | 更新日期: 2023-09-27 18:20:45

CustomRoleProvider中使用时,我的DbContext被处理时遇到问题。

我已经将我的绑定设置为:

kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
kernel.Bind(typeof(IGenericRepository<>)).To(typeof(GenericRepository<>)).InRequestScope();
kernel.Bind<MyContext>().ToSelf().InRequestScope();

我正在使用这个NuGet包Ninject.MVC5 v3.2.1.0,所以Ninject被设置为MVC中的主要DependencyResolver。因此,我能够做这个DependencyResolver.Current.GetService<IGenericRepository<User>>()

但由于某种原因,上下文被处理掉了。

我尝试将此绑定添加到我的设置中:

kernel.Bind<MyContext>().ToSelf().WhenInjectedInto<RoleProvider>();

但这也不起作用。我还尝试通过属性注入进行注入

[Inject]
public IGenericRepository<User> UserRepository { get; set; };

但这只会导致我(还)无法解决的许多其他问题。

据我所知,从错误消息中可以看出,这是由引发的问题。

public abstract class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        User.IsInRole("Admin"); // calls my CustomRoleProvider
        base.OnActionExecuting(filterContext);
    }
}

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...
}

IGenericRepository<T>实现

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
   ...

DbContext已在自定义RoleProvider中释放

显然,RoleProvider的生命周期就是MVC应用程序的整个生命周期。(记不清我在哪里读的了)

因此,必须解决每个方法中的依赖性:(

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>();

我甚至尝试添加InSingletonScope