Ninject.Web.PageBase仍导致对注入依赖项的null引用

Ted*_*Ted 5 asp.net webforms ninject

我有一个使用Ninject 2.0的ASP.NET 3.5 WebForms应用程序.但是,尝试使用Ninject.Web扩展来向System.Web.UI.Page提供注入,即使我切换到使用服务定位器来提供引用,我也会得到对我注入的依赖项的空引用(使用Ninject ),没有问题.

我的配置(为简单而愚蠢):

public partial class Default : PageBase // which is Ninject.Web.PageBase
{
    [Inject]
    public IClubRepository Repository { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        var something = Repository.GetById(1); // results in null reference exception.
    }
 }
Run Code Online (Sandbox Code Playgroud)

... //global.asax.cs

public class Global : Ninject.Web.NinjectHttpApplication
{
    /// <summary>
    /// Creates a Ninject kernel that will be used to inject objects.
    /// </summary>
    /// <returns>
    /// The created kernel.
    /// </returns>
    protected override IKernel CreateKernel()
    {
        IKernel kernel =
        new StandardKernel(new MyModule());
        return kernel;

    }
Run Code Online (Sandbox Code Playgroud)

..

...

public class MyModule : NinjectModule
{
    public override void Load()
    {
        Bind<IClubRepository>().To<ClubRepository>();
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

通过服务定位器获取IClubRepository具体实例工作正常(使用相同的"MyModule").即

  private readonly IClubRepository _repository = Core.Infrastructure.IoC.TypeResolver.Get<IClubRepository>();
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

[更新]最后回到这个,它在Classic Pipeline模式下工作,但不是Integrated.经典管道是否需要?

[更新2]连接我的OnePerRequestModule是问题(为清楚起见,已在上面的示例中删除了):

    protected override IKernel CreateKernel()
    {
        var module = new OnePerRequestModule();
        module.Init(this);

        IKernel kernel = new StandardKernel(new MyModule());

        return kernel;
    }
Run Code Online (Sandbox Code Playgroud)

...需要是:

    protected override IKernel CreateKernel()
    {
        IKernel kernel = new StandardKernel(new MyModule());

        var module = new OnePerRequestModule();
        module.Init(this);

        return kernel;
    }
Run Code Online (Sandbox Code Playgroud)

从而解释了为什么我在集成管道下获得了一个空引用异常(对于Ninject注入的依赖项,或者只是从Ninject.Web.PageBase继承的页面的页面加载 - 无论先出现什么).

ckr*_*mer 2

这是相当令人费解的,因为据我所知,您似乎已正确配置所有内容。从您收到 Null Reference Exception 而不是 ActivationException 的事实来看,页面级注入似乎并未发生。通常,这是由于注入的属性的保护级别所致,但根据您的代码,不存在问题。您可以尝试以下一些方法来帮助找出问题所在:

  1. 对 Kernel.Inject(this) 的调用(启动 Ninject 的属性注入)是在 PageBase 类的 OnInit 方法中完成的。如果由于某种原因此方法没有被执行,则可能会导致您看到的问题。您可以通过重写 RequestActivation() 方法进行进一步的调查,该方法是调用以执行实际注入的方法(请务必调用 base.RequestActivation())。如果您的覆盖从未被调用,则 OnInit 存在问题。

  2. InjectAttribute 是在默认内核配置中设置的,因此不需要指定它,但是如果您想更加确定,可以通过执行以下操作在内核设置中设置属性映射:

    IKernel 内核 = new StandardKernel(new NinjectSettings { InjectAttribute = typeof(InjectAttribute) },new MyModule());

  3. PageBase 类用于注入的内核实例(同样,应该由 Global.asax.cs 中的 CreateKernel 重写实例化的内核实例)存储在 Ninject.Web.KernelContainer 中的服务定位器类型对象中。我将确保您可以在 KernelContainer 上看到 Kernel 属性,并且在 Page_Load 方法中它不为 null。

就洞察力而言,这就是我目前所拥有的一切。就像我说的,从这里看来,你已经把所有的鸭子都打扮好了并排成一排,所以它们应该在工作......

祝你顺利找到问题。