I've found loads of inconclusive articles and questions on how to do property injection on an ActionFilter in ASP.NET MVC3 using Ninject.
Could someone give me a clear example please?
Here's my custom auth attribute.
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
[Inject]
public IService Service { get; set; }
[Inject]
public IAuthenticationHelper AuthenticationHelper { get; set; }
public override void OnAuthorization(AuthorizationContext filterContext)
{
//My custom code
}
}
I am using the WebActivator to set up Ninject
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.Web.AppStart_NinjectMvc3), "Start")]
namespace MyProject.Web {
public static class AppStart_NinjectMvc3 {
public static void RegisterServices(IKernel kernel) {
//Binding things
}
public static void Start() {
// Create Ninject DI Kernel
IKernel kernel = new StandardKernel();
// Register services with our Ninject DI Container
RegisterServices(kernel);
// Tell ASP.NET MVC 3 to use our Ninject DI Container
DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));
}
}
}
My service and helper are never injected. What do I need to change?
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
- this line is not necessaty. NinjectHTTPApplication registersDependencyResolver
inApplication_Start
. – Monica