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
... //global.asax.cs
public class Global : Ninject.Web.NinjectHttpApplication {
..
...
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).
source share