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();
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
source share