2

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 readonly IRepository<Outlet> _outletRepository;

    #endregion

    #region Ctor

    public OutletService(IRepository<Outlet> outletRepository)
    {
        _outletRepository = outletRepository;
    }

    #endregion

    // Rest of class omitted 

CustomAuthorizeAttribute (partial, name changed for this example also)

public class MyAuthorizeAttribute : AuthorizeAttribute
{
    private IOutletService _outletService;
    private IModuleService _moduleService;

    public string Action { get; set; }
    public int Level { get; set; }

    public MarcusAuthorizeAttribute()
    {

    }
    [Inject]
    public MyAuthorizeAttribute(IOutletService outletService, IModuleService moduleService)
    {
        _outletService = outletService;
        _moduleService = moduleService;
    }

I tried using this post as an example, but as soon as I wire it up, none of my routes seem to work (IIS Express returns a 401/cannot find?)
Injecting dependencies into ASP.NET MVC 3 action filters. What's wrong with this approach?

If anyone has any ideas or suggestions, I'd appreciate it! (It's literally driving me up a wall now!)

Thanks!

Community
  • 1
  • 1
Matt Millican
  • 4,044
  • 4
  • 38
  • 55
  • 3
    Try Ninject.MVC3's BindFilter method. [Here's an example][1] [1]: http://stackoverflow.com/questions/7663597/how-to-use-ninject-to-inject-services-into-mvc-3-filterattributes/7663708#7663708 – Brook Dec 24 '11 at 05:05
  • @Brook When I try doing that it kills the app and gives a 401? – Matt Millican Dec 24 '11 at 05:53
  • 1
    For your understanding. Attributes are always created by the .NET framework using a parameterless constructor. Adding a non parameterless ctor is absolute nonesense. – Remo Gloor Dec 24 '11 at 12:03
  • possible duplicate of [Ninject and MVC3: Dependency injection to action filters](http://stackoverflow.com/questions/5078046/ninject-and-mvc3-dependency-injection-to-action-filters) – Remo Gloor Dec 24 '11 at 12:15
  • 1
    there are many other posts on this topic: E.g. http://stackoverflow.com/questions/7014095/help-with-action-filters-in-asp-net-mvc-3/7017819#7017819 – Remo Gloor Dec 24 '11 at 12:16
  • Thanks. I will have to try this more after the holiday! I'm getting 404s on all my routes still. – Matt Millican Dec 24 '11 at 18:40
  • Sorry for all the questions, but I can't seem to get this to work. I looked at the post "Ninject and MVC3: Dependency injection to action filters" referenced above and implemented it as the post said and also did not use the Starter module (Ninject MVC3). The properties that I'm trying to inject into the filter are still _null_ when I am doing a debug. Sorry and thanks for the help so far! – Matt Millican Dec 26 '11 at 19:50

2 Answers2

2

Ninject's MVC extension has a mechanism for injecting dependencies into filters, which is described in the documentation here.

quentin-starin
  • 26,121
  • 7
  • 68
  • 86
-3

You may try this

Filter

public class MyAuthorizeAttribute : AuthorizeAttribute
{
    private IOutletService _outletService;
    private IModuleService _moduleService;

    public string Action { get; set; }
    public int Level { get; set; }

    public MarcusAuthorizeAttribute()
    {
       _outletService = DependencyResolver.Current.GetService<IHelloService>();
       _moduleService  = DependencyResolver.Current.GetService<IModuleService>();
    }
}

Make sure you register your services with dependency resolver you are using.

Praveen Prasad
  • 31,561
  • 18
  • 73
  • 106