I tried the following solution found in How can I implement Ninject or DI on asp.net Web Forms? (Jason's answer)
- Create a new ASP.NET WebForms project
- Use NuGet to add the Ninject.Web lib (which will also bring down the Ninject.Web.Common and Ninject libs)
- Register your custom bindings in App_Start / NinjectWebCommon.cs / RegisterServices method
- Use attribute injection on your pages
Works great in ASP.NET web applications.
The problem is I want to use Ninject for DI in an ASP.NET web site, not a web application.
I registered custom bindings in NinjectWebCommon / RegisterServices:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ITestRepository>().To<TestRepository>();
}
In a web page I inject it:
public partial class _Default : System.Web.UI.Page
{
[Inject]
public ITestRepository _repository { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine(_repository.ExecuteOperation());
}
}
It always throws a null reference exception and the breakpoint inside NinjectWebCommon.cs is never reached - unlike in a web application.
What else should be done to make Ninject 3 work in a web site?