Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6716639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:45:26+00:00 2026-05-26T08:45:26+00:00

I have a Filter and a Filter Provider set in that way: public class

  • 0

I have a Filter and a Filter Provider set in that way:

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)
    {
    }
}

Filter Provider:

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);
}

All my Ninject settings are the same way and are ok, only the DbContext is different: it is InRequestScope():

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

If I put the DbContext in InSingletonScope() all works ok, but DbContext can not stay in SingletonScope, it must to be in RequestScope.

All controllers which depends on DbContext works fine when DbContext is InRequestScope, but when is time to .Net use the Filter it works only on the first Request, on the second request a exception is throw: “The operation cannot be completed because DbContext the has been disposed.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:45:27+00:00Added an answer on May 26, 2026 at 8:45 am

    Filter Providers aren’t created for every request and therefore aren’t allowed to get any dependency in request scope.

    Use https://github.com/ninject/ninject.web.mvc/wiki/Dependency-injection-for-filters instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a filter: class MyFilters { def filters = { before = {
I have a Filter Servlet that filters request from a Servlet. I do not
I have an ISAPI filter that I am using to do URL rewriting for
We have an ASP .NET MVC application that allows users to provide a set
I have a custom exception filter that I'm using to catch a custom exception
I have a set of macros that I have turned into an add-in in
I have a small problem. I have a set of ComboBox's that are bound
I'm trying to find all records in database that have exactly the same set
I have a simple app that consists of: Model Items Filter criteria applied to
I have some filter and one grid.And grid have some buttons like modify/edit.I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.