I have a method attribute which expects several properties to be injected by Ninject 2, but userSession
and jobRepository
are coming up as null:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class JobAttribute : ActionFilterAttribute {
[Inject]
private IUserSession userSession;
[Inject]
private IJobRepository jobRepository;
public override void OnActionExecuting(ActionExecutingContext filterContext) {
var filter = new JobFilter(userSession, jobRepository);
filter.OnActionExecuting(filterContext);
}
}
And here is the method in the controller:
[AcceptGet, Job]
public ActionResult Dimensions(Job job) {
return View(job.Building);
}
I know I have the setup working because if I use constructor injection on the controller the controller's parameters get injected. That doesn't help me much for attributes though that need to use property injection. Am I missing something here?
Here are the pertinent potions of Global.asax.cs:
public class MvcApplication : Ninject.Web.Mvc.NinjectHttpApplication {
protected override void OnApplicationStarted() {
RegisterRoutes(RouteTable.Routes);
RegisterAllControllersIn(Assembly.GetExecutingAssembly());
}
...snip...
protected override IKernel CreateKernel() {
return new StandardKernel(
new RepositoryConfiguration(),
new AuthenticationModule(),
new AutoMapperConfiguration()
);
}
}
public class RepositoryConfiguration : NinjectModule {
public override void Load() {
Bind<ICustomerRepository>().To<CustomerRepository>();
Bind<IJobRepository>().To<JobRepository>();
}
}
public class AuthenticationModule : NinjectModule {
public override void Load() {
Bind<MbdQuote.Core.AppService.IUserSession>().To<UserSession>();
}
}