I also ran into this problem and now I have a working solution. This is similar to the solution described above, but with some slight differences, as well as with the added complete Unity code.
First, I will use property injection for the reason described above, and, as above, I will use the BuildUp method for Unity to embed properties in already created filters.
To do this, I have all my controllers inherited from the new custom base class. In this base class, I override the CreateActionInvoker method to set my own custom ActionInvoker.
Protected Overrides Function CreateActionInvoker() As System.Web.Mvc.IActionInvoker Return CustomActionInvoker End Function
Then in my CustomActionInvoker, I override the GetFilters method.
Protected Overrides Function GetFilters(ByVal controllerContext As ControllerContext, ByVal actionDescriptor As ActionDescriptor) As FilterInfo Dim info = MyBase.GetFilters(controllerContext, actionDescriptor) For Each MyAuthorizationFilter In info.AuthorizationFilters MvcApplication.Container.BuildUp(MyAuthorizationFilter.GetType, MyAuthorizationFilter) Next For Each MyActionFilter In info.ActionFilters MvcApplication.Container.BuildUp(MyActionFilter.GetType, MyActionFilter) Next For Each MyResultFilter In info.ResultFilters MvcApplication.Container.BuildUp(MyResultFilter.GetType, MyResultFilter) Next For Each MyExceptionFilter In info.ExceptionFilters MvcApplication.Container.BuildUp(MyExceptionFilter.GetType, MyExceptionFilter) Next Return info End Function
Unlike the above, I did not find that executing the assembly inside the For Each loop caused any problems. I also overcame the original problem of only having the object referenced through the interface using one of the other overloads of the BuildUp method, which takes System.Type, as well as the existing object.
When doing all of the above, I can now embed dependencies directly in my filters.
Any comments and thoughts are greatly appreciated.
Greetings Mike
Mike dymond
source share