How do you resolve a Ninject dependency in the global file in ASP.NET?

I am using the Ninject and Ninject.Web assemblies with a web forms application. In the global.asax file I specify the bindings like so:

public class Global : NinjectHttpApplication
{
    protected override IKernel CreateKernel()
    {
        IKernel kernel = new StandardKernel();

        // Vendor Briefs. 
        kernel.Bind<IVendorBriefRepository>().To<VendorBriefRepository>().InRequestScope();
        kernel.Bind<IVendorBriefController>().To<VendorBriefController>().InRequestScope();

        // Search Services. 
        kernel.Bind<ISearchServicesController>().To<SearchServicesController>().InRequestScope();
        kernel.Bind<ISearchServicesRepository>().To<SearchServicesRepository>().InRequestScope();

        // Error Logging
        kernel.Bind<IErrorLogEntryController>().To<ErrorLogEntryController>().InRequestScope();
        kernel.Bind<IErrorLogEntryRepository>().To<ErrorLogEntryRepository>().InRequestScope();


        return kernel;
    }
}

Then in my pages I simply have to make them inherit from Ninject.Web.PageBase. Then I can set up properties on the code behind pages and put the [inject] attribute over it.

[inject]
public IVendorBriefController vendorBriefController { get; set; }

This works great. But now I need to do some dependency injection in the Global.asax file itself. I need an instance of IErrorLogEntryController in my Application_Error event. How do I resolve this and use my specified binding for the abstract type?

protected void Application_Error(object sender, EventArgs e)
{
    IErrorLogEntryController = ???;
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Simply do the same thing in your Global class

[Inject]
public IErrorLogEntryController ErrorLogEntryController { get; set; }

NinjectHttpApplication injects itself after the kernel is created.

Method 2

If you’re using the Ninject.Web library for ASP.NET WebForms, you should just be able to get the kernel from the KernelContainer static class. In this library, the KernelContainer class seems to be the starting point for service location using Ninject.

Once you have the kernel, you can get any dependency you need.

protected void Application_Error(object sender, EventArgs e)
{
    IErrorLogEntryController = KernelContainer.Kernel.Get<IErrorLogEntryController>();
}

Method 3

Global.cs:

        HttpContext.Current.Application["UnityContainer"] = System.Web.Mvc.DependencyResolver.Current.GetService(typeof(EFUnitOfWork));


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x