Why I am not able to inject the SetterProperty via StructureMap to an MVC ActionFilter?    
public class LockProjectFilter : ActionFilterAttribute
    {
        [SetterProperty]
        public ISecurityService SecurityService { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var loggedinStaffId = SecurityService.GetLoggedInStaffId();
            if (loggedinStaffId == 1)
                throw new ArgumentNullException();
            base.OnActionExecuting(filterContext);
        }
    }
    public static IContainer Initialize()
    {
        ObjectFactory.Initialize(x =>
                         {
                             x.Scan(scan =>
                                            {
                                                scan.TheCallingAssembly();
                                                scan.WithDefaultConventions();
                                                scan.AssemblyContainingType<ISecurityService>();
                                            });
                             x.SetAllProperties(p => p.OfType<ISecurityService>());
                             //x.ForConcreteType<LockProjectFilter>().Configure
                                // .Setter(c => c.SecurityService).IsTheDefault();
                         });
        return ObjectFactory.Container;
    }
                You need to utilize the 'BuildUp' method off the ObjectFactory.
http://docs.structuremap.net/ConstructorAndSetterInjection.htm#section4
[Test]
    public void create_a_setter_rule_and_see_it_applied_in_BuildUp_through_ObjectFactory()
    {
        var theGateway = new DefaultGateway();
        ObjectFactory.Initialize(x =>
        {
            x.ForRequestedType<IGateway>().TheDefault.IsThis(theGateway);
            // First we create a new Setter Injection Policy that
            // forces StructureMap to inject all public properties
            // where the PropertyType is IGateway
            x.SetAllProperties(y =>
            {
                y.OfType<IGateway>();
            });
        });
        // Create an instance of BuildUpTarget1
        var target = new BuildUpTarget1();
        // Now, call BuildUp() on target, and
        // we should see the Gateway property assigned
        ObjectFactory.BuildUp(target);
        target.Gateway.ShouldBeTheSameAs(theGateway);
    }
Then you can create a new FilterAttributeFilterProvider like this:
public class DependencyResolverFilterProvider : FilterAttributeFilterProvider
{
    public override IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        var filters = base.GetFilters(controllerContext, actionDescriptor);
        foreach (var filter in filters)
        {
            //DI via Setter Injection
            DependencyResolver.BuildUp(filter.Instance);
        }
        return filters;
    }
}
Then finally add your custom filter provider to the .net pipeline.
private static void RegisterProviderAndFilters()
    {
        var oldProvider = FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider);
        FilterProviders.Providers.Remove(oldProvider);
        FilterProviders.Providers.Add(new DependencyResolverFilterProvider());
        RegisterGlobalFilters(GlobalFilters.Filters);
    }
Hope this helps!
wm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With