Injecting Services into Filters

i followed the tutorial on the Ninject-Wiki 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 want to use this pattern :-(

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.

[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:

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:

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

protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); }

Category:c# Views:10 Time:2019-01-06

Related post

  • How to inject services into view/viewmodel using Ninject in MVC3? 2011-08-09

    I am using MVC3 with Ninject, Dependencies in my controller are resolved with no problem. I have few services like localization, format provider and I want them to be injected into view models or Razor views. Right now I am manually injecting them to

  • How to use Ninject to inject services into MVC 3 FilterAttributes? 2011-10-05

    I'm writing a custom ErrorHandler attribute for my MVC project. I would like to inject an implementation of EventViewerLogger into that attribute. I'm using Ninject 2.2 and it works fine for other features, such as injection repositories and aggregat

  • Inject services into an ASP.NET MVC app using Ninject 2011-03-30

    I have Ninject working nicely in an ASP.NET MVC3 web application. I'd like to do a few things when the app starts using a service I've written. Does anyone know how I could inject a service into the MvcApplication itself inside the Global.asax.cs? An

  • How to inject service into Symfony 2 Data Fixtures? 2011-12-30

    How can I inject a service into Symfony2/Doctrine2 Data Fixtures? I want to create dummy users and need the security.encoder_factory service to encode my passwords. I tried defining my Data Fixture as a service myapp.loadDataFixture: class: myapp\Som

  • Injecting Services into a Base Controller class 2012-02-08

    Scenario: An MVC3 app that uses constructor injection to provide services for a given controller: public class HomeController : BaseController { private readonly IArticleService _articleService; public HomeController(IArticleService articleService) {

  • Grails: Inject Service into the Command Object 2012-04-17

    I have a command class which needs to call a service. import org.codehaus.groovy.grails.commons.ApplicationHolder as AH class FilterVisitCommand { def accessRightsService = AH.application.mainContext.accessRightsService def customerService = AH.appli

  • How to use Ninject to inject services into an authorization filter? 2011-06-15

    I am using mvc 3, ninject 2.0 and the ninject mvc 3 plugin. I am wondering how do I get service layers into my filter(in this case an authorization filter?). I like to do constructor inject so is this possible or do I have to property inject?

  • Inject service into an AutoMapper destination class 2011-05-09

    Say I have a source and destination class that is mapped using AutoMapper. The destination has a logger service injected into the constructor. However, I don't know how to get the service injected into the constructor through StructureMap? I've tried

  • How to properly inject service into groovy class? 2011-03-24

    I'm using Grails 1.2.1. I have a class in my src/groovy file in which I'd like to inject a reference to a service. So I have ... def utilityService public HttpTransporter(URI uri, HttpServletResponse response) { this.uri = uri; this.response = respon

  • Injecting dependencies into MVC3 filters 2011-12-24

    I've been having a heck of a time trying to get dependencies injected into a custom authorization filter. OutletService (this is a service I'm trying to inject into my filter) public class OutletService : IOutletService { #region Fields private reado

  • I'm injecting my Service into my controller. Should I inject my Service into my ViewModel instead? 2012-02-23

    So, I've been building a brand new website to replace our current classic asp site. I set up a base project and added in all the different technologies I would be using. Ninject Automapper PagedList EF 4.0 So far I have a working prototype of the web

  • Mvc 3/Unity 2 inject dependencies into a Filter? 2011-05-25

    How can i inject the following dependencies ?? public class Authenticate : AuthorizeAttribute { [Dependency] public IAuthenticate AuthenticateLibrary { get; set; } [Dependency] public ILibrary BaseLibrary { get; set; } protected override bool Authori

  • Can I inject a service into a filter in Grails? 2010-09-29

    I have a service to get and set the user in the session. I would like to pass in some user information to each view if there is a logged in user and thought a filter would be the best way so I don't have to duplicate that in every controller/action.

  • Injecting dependencies into ASP.NET MVC 3 action filters. What's wrong with this approach? 2011-08-25

    Here's the setup. Say I have some action filter that needs an instance of a service: public interface IMyService { void DoSomething(); } public class MyService : IMyService { public void DoSomething(){} } I then have an ActionFilter that needs an ins

  • Injecting a service into an implementation of NHibernate's IUserType using Autofac 2010-10-19

    I'm using NHibernate to map the following class to an Oracle database in my ASP.NET MVC application: public class User { // Needs to be encrypted/decrypted when persisting public virtual string Question { get; set; } } Following several examples that

  • How can I inject something into the session when unit testing a service 2010-12-01

    I have written a service that reads a CAS session variable ... package cp import import javax.servlet.http.HttpSession import org.springframework.web.context.request.RequestContextHolder class AuthorizeServ

  • Ninject - Inject ModelState into Service? 2011-03-11

    I have a controller with a constructor like so: Public Sub New(Service As ICategoryService) The service's constructor looks like this: Public Sub New(Repository As IRepository(Of Category), IValidationDictionary) I have a class Named ModelStateWrappe

  • How do I inject service dependencies into MVC sitemap DynamicNodeProviderBase using StructureMap 2011-07-21

    Consider the following code: public class InboxMenuItemDynamicProvider : DynamicNodeProviderBase { private IMyService _myService { get; set; } public InboxMenuItemDynamicProvider(IActionService actionService) { _myService = myService; } public overri

  • Injecting a Groovy Grails-service into a Java class 2011-11-22

    I have a Grails-service implemented in Groovy, which I'd like to inject into a Java class, in the web application. I know I can get the bean in Java via applicationContext.getBean("exampleService"), but the type ExampleService is unknown at compile t

  • Injecting factory into service fails 2014-07-06

    I've setup an angular seed project with some services and a factory. companyService depends on a factory called company. Injecting company into companyService fails with this error. I can't seem to figure out what I'm doing wrong. var module = angula

  • Symfony2/JmsDIExtraBundle Injecting repository into service using annotations 2015-02-16

    In my project I’m using annotations with JMSDIExtraBundle. My question is: how I can tell my app that repository should be service so, I can inject it in another service using annotations. Only way I know is using XML file to define repository as ser

  • How do you use Autofac to inject setters into your action filters in ASP.NET MVC2? 2010-06-26

    I thought this would do the trick in my Global.asax Application_Start, but it doesn't work: var builder = new ContainerBuilder(); builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>(); builder.RegisterControllers(Assembly.G

  • MVC/Unity - How to inject dependencies into custom FilterAttributes? 2009-06-23

    I Have a custom HandleErrorAttribute that extends FilterAttribute. How can I have Unity inject dependencies into this attribute at the same time that it injects the controller's dependencies itself? --------------Solutions------------- You have two o

  • How to do Spring.NET DI injection in action filters (ASP.NET MVC 3) 2011-09-29

    Tried to search but no specific answers (I am very new in this)... With Spring.NET supporting ASP.NET MVC 3, how can I do dependency injection inside action filters? Studied around but I have no clue at all! :/ On Spring.NET documentary, it appears t

  • Injecting dependencies into AuthorizeAttribute in a class library 2012-01-06

    I've run into an interesting design issue with a class library I am writing. I have a custom implementation of the AuthorizeAttribute that I want clients to be able to use like this: [Protected("permission_name")] In the above code, PermissionAttribu

Copyright (C), All Rights Reserved.

processed in 0.164 (s). 11 q(s)