Hi I use MS Unity Container with my MVC 3 application.
I want to use setter injection to inject a dependency for Action filter. I have an action filter which I am registering in global.asax and this action Filter calls a repository class to load some data. I want to inject concrete class using DI. It can be very easily achieved with StructureMap. But somehow I can't get it working with Unity. I have tried all solutions from this link.
Here is my code:
MyActionFilter.cs
public class GlobalViewBagActionFilter : ActionFilterAttribute
{
public IUserRepo UserRepo { get; set; }
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
var testVal = UserRepo.LoadData();
}
}
Registering Action Filter in Global.asax.cs
private static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new GlobalViewBagActionFilter());
}
calling the above method on Appliation_Start to register
RegisterGlobalFilters(GlobalFilters.Filters);
I have tried many possible code block for registering the class but nothing worked. Either I would get an exception or on the action filter, UserRepo property would be null.
Any pointers on how do i get it working?