Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

i followed the tutorial on https://github.com/ninject/Ninject.Web.Mvc/wiki/Dependency-injection-for-filters
It is driving me insane, the service inside the filter is null and won't change to an object instance. I tried property Injection and decorated the interface with the [Inject]-attribute - no results.
I tried injection via constructor injection (as seen below) - the filter isn't even hit! I don't know how else i could inject the ILogger-Service into a filter :-(

Please for the love of god (and my sanity) help me resolve this problem!

Best regards

I have got a "BaseController", that serves as a central Baseclass for all my controllers. It is decorated with the "MyExceptionLog"-Attribute.

C#
[MyExceptionLog]
    public class BaseController : Controller
    {
       //omitted for brevity
    }


The "MyExceptionLog" is just a empty class to mark controllers. It inherits from FilterAttribute.
The filter i want to have my service injected looks as follows:

C#
public class MyExceptionLogFilter:ActionFilterAttribute
    {
        private readonly ILogger Logger;
        public MyExceptionLogFilter(ILogger logger)
        {
            Logger = logger;
        }
        public override void  OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext.Exception != null)
            {
                Logger.Log(nameof(filterContext.Controller), filterContext.Exception, null);
                if (filterContext.Exception.InnerException != null)
                {
                    Logger.Log(nameof(filterContext.Controller), filterContext.Exception.InnerException, null);
                }
            }
        }
    }


I use a customControllerFactory to inject services into my controllers:
C#
public class NinjectControllerFactory : DefaultControllerFactory
    {
        public IKernel kernel;

        public NinjectControllerFactory()
        {
            kernel = new StandardKernel();
            AddBindings();
        }

        private void AddBindings()
        {
            kernel.Bind<IDbRepository>().To<DbRepository>();
            kernel.Bind<ILogger>().To<EntLibLogger>();
            kernel.BindFilter<MyExceptionLogFilter>(FilterScope.Action, 0).WhenControllerHas<MyExceptionLogAttribute>();
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            return controllerType == null
                ? null
                : (IController)kernel.Get(controllerType);
        }
    }


And finally i register the controllerFactory in my Global.Asax.cs
C#
protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
        }
Posted
Updated 23-Jun-15 3:42am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900