How do I embed asp.net http in some generic http handler using Ninject?

I'm a newbie using Ninject and I can't figure out how to inject http into my generic handler. I have an MVC3 project and I inject my services into controllers without any problems. This is what I got in my Ninject App_start class for registering services:

private static void RegisterServices(IKernel kernel) { kernel.Bind<NLSubscriber.Core.Service.Repository.INLUserRepository>().To<NLSubscriber.Core.Service.Repository.EFDAL.EFNLUserRepository>().InRequestScope(); kernel.Bind<Neticon.Mvc.Helpers.IConfigHelper>().To<Neticon.Mvc.Helpers.AzureEnabledConfigHelper>().InSingletonScope(); kernel.Bind<Neticon.Security.Service.IAuthenticationService>().ToMethod(m => Neticon.Security.Service.SecurityServiceFactory.CreateAuthenticationService()).InRequestScope(); kernel.Bind<Neticon.Security.Service.IMembershipService>().ToMethod(m => Neticon.Security.Service.SecurityServiceFactory.CreateMembershipService()).InRequestScope(); kernel.Bind<Neticon.Security.Service.IRoleManagerService>().ToMethod(m => Neticon.Security.Service.SecurityServiceFactory.CreateRoleManagerService()).InRequestScope(); 

When I try to get some service from my common handler using property injection (with the [inject] attribute), I always get null. This is what my handler looks like:

  public class SubscriberHandler : IHttpHandler { [Inject] public INLUserRepository userRep { get; set;} public void ProcessRequest(HttpContext context) { var users = userRep.GetUsers(); //userRep is always null here } public bool IsReusable { get { return false; } } } 

I also tried to do it like this:

  readonly INLUserRepository userRep; public SubscriberHandler() { using (IKernel kernel = new StandardKernel(new App_Start.NJRepositoryModule())) { userRep = kernel.Get<INLUserRepository>(); } } 

but I get an exception: "Error loading the ICache component in Ninject. Such a component was not registered in the kernel component container. Suggestions: 1) If you created a custom subclass for KernelBase, make sure that you correctly implement the AddComponents () method. 2) Make sure that you did not remove the component from the container by calling RemoveAll (). 3) Make sure that you do not accidentally create more than one kernel.

This suggests that I should not instantiate more than one core in my application, right? What am I doing wrong? Thanks

+6
source share
2 answers

You can use the dependency resolver:

 public class SubscriberHandler : IHttpHandler { public INLUserRepository userRep { get; private set; } public SubscriberHandler() { userRep = DependencyResolver.Current.GetService<INLUserRepository>(); } public void ProcessRequest(HttpContext context) { var users = userRep.GetUsers(); //userRep is always null here } public bool IsReusable { get { return false; } } } 

I expect to get negative feedback from this answer because the service locator pattern is seen by many as an anti-pattern.

But I'm not sure that NInject allows you to use the constructor installation for HTTP handlers, since they are created in the ASP.NET runtime.

+3
source

The root of the composition for IHttpHandler is IHttpHandlerFactory . You can create a custom IHttpHandlerFactory that uses Ninject to instantiate your IHttpHandler . This way you can use constructor injection.

+5
source

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


All Articles