DbContext已置于自定义RoleProvider中

时间:2015-11-06 10:13:48

标签: c# asp.net-mvc entity-framework ninject

我的DbContext在我的CustomRoleProvider中使用时遇到了问题。

我设置了我的绑定:

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

1 个答案:

答案 0 :(得分:3)

显然,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

相关问题