title: Autofac MVC4 Action Filter Injection causes infinite loop
body: <p>I am running into an issue with Action Filters and Autofac. I have an action filter which uses a dependency to determine if the request should be redirected or not. I am applying this filter explicitly to only a sub set of controller actions. The redirect will direct to an action does not have this filter applied. However, the filter still triggers and I end up in an endless loop.</p>
  
  <p><strong>Please note</strong> that the code below is a skeleton of some of the logic going on. I just want to capture the problem I'm having.</p>
  
  <p>In my case the controller that has this atribute is the build in accound controller. Namely the Login action. I have set up the autorization filter to apply to all actions in the FitlerConfig.cs:</p>
  
  <pre><code>public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  {
      filters.Add(new AuthorizeAttribute());
      filters.Add(new HandleErrorAttribute());
  }
  </code></pre>
  
  <p>My Filter Looks like this:</p>
  
  <pre><code>public class CustomFilter : ActionFilterAttribute
  {
      public SomeDependancy MyDependancy { get; set; }
  
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          if (MyDependancy.ShouldRedirect)
          {
              filterContext.Result = new RedirectToRouteResult("Default",
                  new RouteValueDictionary
                  {
                      { "controller", "Error" }, { "action", "CustomError" }
                  }
              );
          } 
  
          base.OnActionExecuting(filterContext);
          return;
      }
  }
  </code></pre>
  
  <p>My Account Controller:</p>
  
  <pre><code>public class AccountController : Controller
  {
      [AllowAnonymous]
      [CustomFilter]
      public ActionResult Login(string returnUrl)
      {
          ViewBag.ReturnUrl = returnUrl;
          return View();
      }
  }
  </code></pre>
  
  <p>Error Controller:</p>
  
  <pre><code>public class ErrorController : Controller
  {
      [AllowAnonymous]
      public ActionResult CustomError()
      {
          return View("CustomError");
      }
  }
  </code></pre>
  
  <p>And finally, my autofac registrations:</p>
  
  <pre><code>var builder = new ContainerBuilder();
  
  builder.Register(con =&gt; new SomeDependancy()).AsSelf().InstancePerHttpRequest();
  
  builder.RegisterType&lt;CustomFilter&gt;().As&lt;IActionFilter&gt;().PropertiesAutowired();
  
  // property injection on filters
  builder.RegisterFilterProvider();
  
  // Needed to allow property injection in custom action filters.
  builder.RegisterType&lt;ExtensibleActionInvoker&gt;().As&lt;IActionInvoker&gt;();
  
  builder.RegisterControllers(typeof(MvcApplication).Assembly).InjectActionInvoker();
  
  var container = builder.Build();
  DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  </code></pre>
  
  <p>What ends up happening is if I set a break point in my CustomFilter, when I go to the login page, the breakpoint hits and the redirect occurs, then the break point hits again, and again, and again. What is baffling me is the fact that this action filter is not applied to that controller or action.</p>
  
  <p>If I comment out the dependency and simply redirect in the filter and then comment out autofac registrations, things work as expected. Upon hitting the login screen the filter triggers and redirects to the error page. </p>