问题描述
我使用Ninject一个新手,我无法弄清楚如何注入到我的通用HTTP处理程序。我有一个MVC3项目,我都注入我的服务到控制器,没有问题。
这是我在Ninject App_start类得到了注册服务:
I'm a newbie using Ninject and I can't figure out how to inject into my generic http handler. I have a MVC3 project and I'm injecting my services into controllers with no problem at all.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 generic handler by using property injection (with [inject] attribute) I always get null. This is how 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 have also tried doing it like this:
readonly INLUserRepository userRep;
public SubscriberHandler()
{
using (IKernel kernel = new StandardKernel(new App_Start.NJRepositoryModule()))
{
userRep = kernel.Get<INLUserRepository>();
}
}
但我发现了异常:错误加载Ninject组件ICACHE没有这样的组件已在内核中的组件容器注册。
建议:
1)如果你已经创建了KernelBase自定义子类,请确保您有正确
实施AddComponents()方法。
2)确保你没有通过调用删除从容器中的组件removeall过()。
3)确保你不小心多个内核中创建的。
but I'm getting an exception: "Error loading Ninject component ICache. No such component has been registered in the kernel's component container.Suggestions: 1) If you have created a custom subclass for KernelBase, ensure that you have properly implemented the AddComponents() method. 2) Ensure that you have not removed the component from the container via a call to RemoveAll(). 3) Ensure you have not accidentally created more than one kernel."
这是暗示我,我不应该在我的应用程序创建一个以上的内核,对吧?
我究竟做错了什么?
谢谢
That's suggesting me that I'm not supposed to instantiate more than one kernel in my application, right?What am I doing wrong?Thanks
推荐答案
您可以使用依赖解析器:
You could 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;
}
}
}
我期待,因为Service Locator模式被许多人认为是一个反模式,从这个答案得到负面的反馈。
I am expecting to get negative feedback from this answer because the service locator pattern is considered by many as an anti-pattern.
但我不知道是否NInject允许您使用构造函数注射HTTP处理程序,因为它们是由ASP.NET运行时实例。
But I am not sure whether NInject allows you to use constructor injection for HTTP handlers because they are instantiated by the ASP.NET runtime.
这篇关于我如何注入使用Ninject一些通用的asp.net HTTP处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!