gpt4 book ai didi

c# - DbContext 已在自定义 RoleProvider 中处置

转载 作者:太空宇宙 更新时间:2023-11-03 23:26:52 24 4
gpt4 key购买 nike

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

我已将我的绑定(bind)设置为:

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

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

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

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

但这也行不通。我还尝试通过属性注入(inject)进行注入(inject)

[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

关于c# - DbContext 已在自定义 RoleProvider 中处置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33564359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com