c# – 使用IAuthorizationFilter和Ninject和EF给DbContext配置错误

我正在尝试在IAuthorizationFilter的实现中使用我的UnitOfWork,但是在我在几个页面之间导航后,我得到了这个异常:

System.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.

FilterConfig.cs

filters.Add(DependencyResolver.Current.GetService(typeof(PermissionFilter)));

NinjectMappings.cs

public class NinjectMappings : NinjectModule
{
    public override void Load()
    {
        Bind<MyContext>().ToSelf().InRequestScope();
        Bind<IUnitOfWork>().To<UnitOfWork>();
    }
}

PermissionFilter.cs

public class PermissionFilter : IAuthorizationFilter
{
    public PermissionFilter(IUnitOfWork unitOfWork)
    {
        // etc...
    }
}

我能够解决这个问题:

// NinjectMappings
Bind<IUnitOfWork>()
    .ToMethod(m => GetUnitOfWork())
    .WhenInjectedExactlyInto(typeof(PermissionFilter));

private IUnitOfWork GetUnitOfWork()
{
    return new UnitOfWork(new MyContext());
}

现在的问题是GetUnitOfWork只在应用启动时调用一次.我尝试在InTransientScope和InRequestScope之间交替无济于事.因此,不会检索数据库的更新,而是我的UnitOfWork始终返回相同的数据.

我已经阅读了很多处理DbContext的问题,但是他们都没有实现IAuthorizationFilter.

解决这个问题的最佳方法是什么?我想避免使用过滤器中的new或using()或使用Service Locator模式.

最佳答案 您的问题来自于MyContext在请求范围内实例化并在请求结束时处理.

要解决这个问题,避免使用new(),using或ServiceLocator模式,您可以依赖专用的IFilterProvider

public class PermissionFilterProvider : IFilterProvider
{
    private readonly Func<PermissionFilter> _permissionFilterFactory = null;

    public PermissionFilterProvider(Func<PermissionFilter> filterFactory)
    {
        _permissionFilterFactory = filterFactory;
    }

    public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        var filters = new List<Filter>(); 

        // instantiate PermissionFilter action filter  
        filters.Add(new Filter(_permissionFilterFactory(), FilterScope.Action, 0));
        return filters;
    }
}

您的绑定将是:

public class NinjectMappings : NinjectModule
{
    public override void Load()
    {
        Bind<MyContext>().ToSelf().InRequestScope();
        Bind<IUnitOfWork>().To<UnitOfWork>();
        Bind<IFilterProvider>().To<PermissionFilterProvider>();
        Bind<PermissionFilter>().ToSelf();
    }
}

请注意,您将需要像Ninject.Extensions.Factory这样的Ninject Factory扩展来实现Func< PermissionFilter> PermissionFilterProvider构造函数中的工厂模式.

另外,我不确定您的IUnitOfWork的范围.它不应该与你的MyContext相同吗?

点赞