RequestScope中使用Ninject抛出的DbContext:“操作无法完成,因为已经处理了DbContext。”

时间:2011-10-19 16:43:56

标签: .net ninject action-filter

我以这种方式设置了过滤器和过滤器提供程序:

public class AuditoriaFilter : IActionFilter
{
    #region Fields

    private readonly IRegistrarAuditoriaService _registrarAuditoriaService;

    private readonly IUsuarioRepository _usuarioRepository;

    private readonly ISiteRepository _siteRepository;

    private readonly IModuloRepository _moduloRepository;

    private readonly IAtividadeRepository _atividadeRepository;

    #endregion

    #region Constructors

    public AuditoriaFilter(IRegistrarAuditoriaService registrarAuditoriaService, IUsuarioRepository usuarioRepository, ISiteRepository siteRepository, IModuloRepository moduloRepository, IAtividadeRepository atividadeRepository)
    {
        this._registrarAuditoriaService = registrarAuditoriaService;
        this._usuarioRepository = usuarioRepository;
        this._siteRepository = siteRepository;
        this._moduloRepository = moduloRepository;
        this._atividadeRepository = atividadeRepository;
    }

    #endregion

    public void OnActionExecuted(ActionExecutedContext context)
    {
        Usuario usuario = _usuarioRepository.GetById(1);

        Site site = _siteRepository.GetById(1);

        Modulo modulo = _moduloRepository.GetById(1);

        Atividade atividade = _atividadeRepository.GetById(1);

        _registrarAuditoriaService.Registrar(usuario, site, modulo, atividade);
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
    }
}

过滤器提供商:

public class AuditoriaFilterProvider : IFilterProvider
{
    #region Fields

    private readonly IRegistrarAuditoriaService _registrarAuditoriaService;

    private readonly IUsuarioRepository _usuarioRepository;

    private readonly ISiteRepository _siteRepository;

    private readonly IModuloRepository _moduloRepository;

    private readonly IAtividadeRepository _atividadeRepository;

    #endregion

    #region Constructors

    public AuditoriaFilterProvider(IRegistrarAuditoriaService registrarAuditoriaService, IUsuarioRepository usuarioRepository, ISiteRepository siteRepository, IModuloRepository moduloRepository, IAtividadeRepository atividadeRepository)
    {
        this._registrarAuditoriaService = registrarAuditoriaService;
        this._usuarioRepository = usuarioRepository;
        this._siteRepository = siteRepository;
        this._moduloRepository = moduloRepository;
        this._atividadeRepository = atividadeRepository;
    }

    #endregion

    public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        bool postHttpMethod = controllerContext.RequestContext.HttpContext.Request.HttpMethod.ToUpper() == "POST";
        string controllerName = actionDescriptor.ControllerDescriptor.ControllerName;

        if (postHttpMethod && controllerName != "Login")
            yield return new Filter(new AuditoriaFilter(_registrarAuditoriaService, _usuarioRepository, _siteRepository, _moduloRepository, _atividadeRepository), FilterScope.Action, null);

        yield break;
    }
}

的Global.asax:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    AuditoriaFilterProvider auditoriaFilterProvider = new AuditoriaFilterProvider(DependencyResolver.Get<IRegistrarAuditoriaService>(), DependencyResolver.Get<IUsuarioRepository>(), DependencyResolver.Get<ISiteRepository>(), DependencyResolver.Get<IModuloRepository>(), DependencyResolver.Get<IAtividadeRepository>());

    FilterProviders.Providers.Add(auditoriaFilterProvider);
}

我的所有Ninject设置都是一样的,没问题,只有DbContext不同:它是InRequestScope()

public override void Load()
{
    Bind<IDbContext>().To<DbContext>().InRequestScope().WithConstructorArgument("connectionString", ConfigurationManager.AppSettings["DefaultConnectionString"]);
}

如果我将DbContext放入InSingletonScope()一切正常,但DbContext无法留在SingletonScope中,则必须在RequestScope中。

当DbContext是InRequestScope时,依赖于DbContext的所有控制器都能正常工作,但是什么时候.Net使用Filter只能在第一个Request上工作,在第二个请求时抛出异常:“操作不能完成,因为已经处理了DbContext。“

1 个答案:

答案 0 :(得分:2)

不会为每个请求创建过滤器提供程序,因此不允许在请求范围内获得任何依赖项。

改为使用https://github.com/ninject/ninject.web.mvc/wiki/Dependency-injection-for-filters