Structuremap 4.2.0.402 web api filter property setter injection

162 views
Skip to first unread message

Atul Chaudhary

unread,
Jul 1, 2016, 7:36:45 AM7/1/16
to structuremap-users
HI Guys,

After doing a lot of googling I cannot find the implementation of property injection in web api filter.

I have created my logging ActionFilterAttribute and trying to do the property injection for my repository but cannot find any documentation and help on how to set up the config.

Any help will be really appreciated

Regards

Vahid N.

unread,
Jul 2, 2016, 2:58:07 AM7/2/16
to structuremap-users
First add this class
    public class SmWebApiFilterProvider : ActionDescriptorFilterProvider, IFilterProvider
   
{
       
private readonly IContainer _container;

       
public SmWebApiFilterProvider(IContainer container)
       
{
            _container
= container;
       
}

       
public new IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
       
{
           
var filters = base.GetFilters(configuration, actionDescriptor);

           
foreach (var filter in filters)
           
{
                _container
.BuildUp(filter.Instance);
               
yield return filter;
           
}
       
}
   
}
And then register it:
    public static class WebApiConfig
   
{
       
public static void Register(HttpConfiguration config)
       
{
           
var container = /* your container */;

            config
.Services.Replace(typeof(IFilterProvider), new SmWebApiFilterProvider(container));



Atul Chaudhary

unread,
Jul 2, 2016, 4:02:40 AM7/2/16
to structuremap-users
I tried this approach and got one step ahead now my break point on getFlters method getting hit but still my filter property is null

Code for my filter is below

public class MvcLogFilterAttribute : ActionFilterAttribute
   
{
       
public ISwiftDemoUow uow { get; set; }
       
public MvcLogFilterAttribute()
       
{


       
}
       
public MvcLogFilterAttribute(ISwiftDemoUow _uow)
       
{
           
this.uow = _uow;
       
}
       
public override void OnActionExecuting(ActionExecutingContext filterContext)
       
{
           
var gg = uow;
           
base.OnActionExecuting(filterContext);
       
}
   
}
my uow is still null

Atul Chaudhary

unread,
Jul 2, 2016, 4:24:59 AM7/2/16
to structuremap-users
Thanks I got it working all I have to do is add following fluent config
c.Policies.SetAllProperties(x =>
                    x
.Matching(p =>
                        p
.DeclaringType.CanBeCastTo(typeof(System.Web.Http.Filters.ActionFilterAttribute)) &&
                        p
.DeclaringType.Namespace.StartsWith("MyProjectName") &&
                       
!p.PropertyType.IsPrimitive &&
                        p
.PropertyType != typeof(string)));

Vahid N.

unread,
Jul 2, 2016, 5:03:09 AM7/2/16
to structuremap-users
Now remove the constructors and also define your dependency as a Func<ISwiftDemoUow> to make it transient. otherwise it won't be instantiated more than one time.

Atul Chaudhary

unread,
Jul 2, 2016, 5:48:46 AM7/2/16
to structuremap-users
Thanks for your help and extra advice I will post my sample soon as it is very hard to find the proper working solution.


Regards,
Atul
Reply all
Reply to author
Forward
0 new messages