title: Using Unity to inject dependencies into a custom ActionFilter
body: <p>At the moment, I have a custom ControllerFactory into which I inject my Unity container:</p>
  
  <p>in global.asax Application_Start():</p>
  
  <pre><code>var container = InitContainer();
  DependencyResolver.SetResolver(new UnityDependencyResolver(container));
  
  var factory = new UnityControllerFactory(container);
  ControllerBuilder.Current.SetControllerFactory(factory);
  </code></pre>
  
  <p>In the controller factory I set my controllers to use a custom ActionInvoker like so:</p>
  
  <pre><code>protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
  {
      var controller = base.GetControllerInstance(requestContext, controllerType) as Controller;
  
      if (controller != null)
          controller.ActionInvoker = new UnityActionInvoker(_container);
  
      return controller;
  }
  </code></pre>
  
  <p>Finally in my custom ActionInvoker, I attempt to buildup actions being invoked using the ActionInvokers container:</p>
  
  <pre><code>protected override ActionExecutedContext InvokeActionMethodWithFilters(
          ControllerContext controllerContext,
          IList&lt;IActionFilter&gt; filters,
          ActionDescriptor actionDescriptor,
          IDictionary&lt;string, object&gt; parameters)
  {
      var builtUpFilters = new List&lt;IActionFilter&gt;();
  
      foreach (IActionFilter actionFilter in filters)
      {
          builtUpFilters.Add(_container.BuildUp&lt;IActionFilter&gt;(actionFilter));
      }
  
      return base.InvokeActionMethodWithFilters(controllerContext, builtUpFilters, actionDescriptor, parameters);
  }
  </code></pre>
  
  <p>Here is an example of one of the ActionFilters that is being built up:</p>
  
  <pre><code>public class PopulatRolesAttribute : ActionFilterAttribute, IActionFilter
  {
      private const string RolesKey = "roles";
  
      [Dependency]
      public Func&lt;IMetadataService&gt; Service { get; set; }
  
      public PopulatRolesAttribute()
      {
      }
  
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          if (filterContext.Controller.ViewData[RolesKey] == null)
          {
              filterContext.Controller.ViewData[RolesKey] = Service().GetRoles();
          }
      }
  }
  </code></pre>
  
  <p>The problem is that the public property on my custom ActionFilterAttribute is never injected with anything, it remains null on execution! I cannot see why my filter is not being correctly builtup by the container. The type being injected is registered properly, like so:</p>
  
  <pre><code>container.RegisterInstance(new ChannelFactory&lt;IMetadataService&gt;(
      new BasicHttpBinding(),
      new EndpointAddress("http://example.com/ABSApplication/MetadataService.svc")));
  
  container.RegisterInstance&lt;Func&lt;IMetadataService&gt;&gt;(
      () =&gt; container.Resolve&lt;ChannelFactory&lt;IMetadataService&gt;&gt;().CreateChannel());
  </code></pre>
  
  <p>And is also being injected elsewhere in the application (Although not via .Buildup). This is pretty much the same process followed by this <a href="http://codeclimber.net.nz/archive/2009/02/10/how-to-use-ninject-to-inject-dependencies-into-asp.net-mvc.aspx" rel="noreferrer">blog post</a>. What piece of the puzzle am I missing?</p>