简体   繁体   English

Ninject 2属性注入ActionFilterAttribute不起作用

[英]Ninject 2 Property Injection for ActionFilterAttribute not working

I have a method attribute which expects several properties to be injected by Ninject 2, but userSession and jobRepository are coming up as null: 我有一个方法属性,它期望Ninject 2注入几个属性,但userSessionjobRepository会出现为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: 以下是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>();
    }
}

Ninject2 does not do field injection, change them to properties with a public setter. Ninject2不进行字段注入,使用公共setter将它们更改为属性。

class JobAttribute : ActionFilterAttribute {
    [Inject]
    public IUserSession UserSession
    { set; private get; }

    [Inject]
    public IJobRepository JobRepository
    { set; private get; }
}

From the Ninject 2 Beta announcement : 来自Ninject 2 Beta 公告

Things that were in Ninject 1.x that are not in Ninject 2: Ninject 1.x中没有出现在Ninject 2中的东西:

  • Field injection: Ninject 2's injection is now driven by expression trees, and in .NET 3.5 there is no way to set field values with an expression tree. 现场注入:Ninject 2的注入现在由表达式树驱动,而在.NET 3.5中,无法使用表达式树设置字段值。 Since this is a bad practice anyway, I decided to cut it. 因为这是一个不好的做法,我决定削减它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM