簡體   English   中英

DbContext已置於自定義RoleProvider中

[英]DbContext has been disposed in custom RoleProvider

在我的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
   ...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM