Action filter constructor being called repeatedly

2019-09-07 05:03发布

问题:

I'm using an action filter on some of my controllers, irrelevant what it does, that uses Ninject for dependency injection. I set breakpoints in the controller constructor and the action filter constructor, and I found that for a single request, the controller's constructor gets called once and the action filter's 8 times. The following code produces the effect, virtually the same as that given in this answer.

Attribute:

public class NotificationAttribute : Attribute { }

public class NotificationActionFilter : IActionFilter
{
    private IUnitOfWork _unitOfWork;

    public NotificationActionFilter(IUnitOfWork uow)
    {
        _unitOfWork = uow;
    }

    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        // do stuff
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {

    }
}

Binding:

kernel.BindFilter<NotificationActionFilter>(FilterScope.Controller, 0).WhenControllerHas<NotificationAttribute>();

Controller:

[NotificationAttribute]
public class TestController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

It's always exactly 8, but I have no idea why. I've really been pulling my hair out over this one, just hoping someone might have dealt with something similar before.