Ninject.Web.PageBase still leads to null reference to nested dependency

I have an ASP.NET 3.5 WebForms application using Ninject 2.0. However, trying to use the Ninject.Web extension to provide an injection in System.Web.UI.Page, I get a null reference to my nested dependency, even if I switch to using the service locator to provide the link (using Ninject), there is no problem.

My configuration (omitted for simplicity):

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. } } 

... //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; } 

..

...

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

Getting a specific instance of IClubRepository through the service locator works fine (uses the same "MyModule"). I.e

  private readonly IClubRepository _repository = Core.Infrastructure.IoC.TypeResolver.Get<IClubRepository>(); 

What am I missing?

[Update] Finally returned to this, and it works in classic pipeline mode, but not integrated. Is classic piping required?

[Update 2] Posting my OnePerRequestModule was a problem (which was fixed in the example above for clarity):

  protected override IKernel CreateKernel() { var module = new OnePerRequestModule(); module.Init(this); IKernel kernel = new StandardKernel(new MyModule()); return kernel; } 

... it should be:

  protected override IKernel CreateKernel() { IKernel kernel = new StandardKernel(new MyModule()); var module = new OnePerRequestModule(); module.Init(this); return kernel; } 

Thus, I explain why I got a null-reference exception in the integrated pipeline (to a Ninject-dependent or to loading a page for a page that inherits from Ninject.Web.PageBase - everything that came first).

+4
source share
2 answers

This is rather perplexing, because from what I can say, it seems that everything is set up correctly for you. From the fact that you get a Null Reference Exception instead of an ActivationException, it looks like page-level injection does not seem to be happening. Typically, this is due to the level of protection of the input property, but based on your code there are no problems. Here are some tips you can try to figure out what the problem is:

  • A call to Kernel.Inject (this), which initiates the injection of properties for Ninject, is made in the OnInit method of the PageBase class. If for some reason this method does not work, it may lead to a problem in your vision. You can continue your research by overriding the RequestActivation () method, which is the method called for the actual injection (be sure to call base.RequestActivation ()). If your override is never called, then there is a problem with OnInit.

  • InjectAttribute is configured in the kernel configuration by default, so you do not need to specify it, however, if you want to be more specific, you can configure attribute mapping in your kernel, configured something like:

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

  • The kernel instance used by the PageBase class for injection (as well as the one that must be created using the CreateKernel override in your Global.asax.cs file) is stored in an object of the service locator type in Ninject.Web.KernelContainer. I would make sure that you see the Kernel property on the KernelContainer and that it is not empty of your Page_Load method.

That’s all I have at the moment, as far as I understand. As I said, it can be seen from here that you have all your ducks dressed and set in rows, so they should work ...

Good luck tracking the problem.

+2
source

This may not be typical for Ninject. I can get the same exception in integrated mode without IoC. I just have a simple asp.net application that just contains one aspx page without logic.

In my global.asax file, I have the following:

 public class Global : HttpApplication { protected void Application_Start(object sender, EventArgs e) { this.EndRequest += new EventHandler(Global_EndRequest); } void Global_EndRequest(object sender, EventArgs e) { // do stuff } 

Basically subscribing to an event in application_start raises this exception for me when working in integrated pipeline mode. Going to the classic pipeline or deleting the subscription and event handler makes the error go away. I am running IIS 7.5 on Win7 Enterprise 64bit.

This may not solve your specific problem, but I am posting here as this is the only page that appeared when I inserted the exception in google! I will translate my answer into a separate question when I am allowed to ask it. I don't have stackoverflow flaws yet :(

+1
source

Source: https://habr.com/ru/post/1302780/


All Articles