使用现有的自定义库从 HttpHandler 访问 Ninject Kernel.Get()
我有一个 ASP.Net webforms 应用程序,它使用 Ninject 2.2.0.0
我有一个继承自 Microsoft.Web.ImageHandler 类的 HTTPHandler。
在其中我需要访问我创建的服务类的实例。
因为我无法从 Ninject.Web.HttpHandlerBase 继承,所以我想我只需将内核公开为 Global.asax 类上的属性...
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel(new DefaultModule());
var sms = kernel.Get<SiteMapService>();
SiteMapSvc = sms;
Kernel = kernel;
return kernel;
}
public IKernel Kernel
{
get; set;
}
并使用 kernel.Get 方法来获取服务。
var global = (Global) HttpContext.Current.ApplicationInstance;
var service = global.Kernel.Get<PhotoService>();
这将失败,并显示以下内容..
[ArgumentNullException: Cannot be null
Parameter name: root]
Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:258
Ninject.ResolutionExtensions.Get(IResolutionRoot root, Type service, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:151
Thumb.GenerateImage(NameValueCollection parameters) in \Thumb.ashx.cs:40
更新: 我设法通过修改 Global.Kernel 属性来解决此问题,但现在我进入 反模式领域...
public IKernel Kernel
{
get { return this.CreateKernel(); }
}
现在将阅读并了解这意味着什么..
I have an ASP.Net webforms app, that uses Ninject 2.2.0.0
I have a HTTPHandler that inherits from the Microsoft.Web.ImageHandler class.
Within it i need to access an instance of a service class that i created.
because i cannot inherit from Ninject.Web.HttpHandlerBase i thought i would just expose the Kernel as a property on the Global.asax class...
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel(new DefaultModule());
var sms = kernel.Get<SiteMapService>();
SiteMapSvc = sms;
Kernel = kernel;
return kernel;
}
public IKernel Kernel
{
get; set;
}
and use the kernel.Get method to obtain the service..
var global = (Global) HttpContext.Current.ApplicationInstance;
var service = global.Kernel.Get<PhotoService>();
This fails with the following...
[ArgumentNullException: Cannot be null
Parameter name: root]
Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:258
Ninject.ResolutionExtensions.Get(IResolutionRoot root, Type service, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:151
Thumb.GenerateImage(NameValueCollection parameters) in \Thumb.ashx.cs:40
UPDATE:
I managed to fix this by modifying the Global.Kernel property to this, but now im getting into anti pattern territory...
public IKernel Kernel
{
get { return this.CreateKernel(); }
}
Will now read up and see what this means..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用 Service-Locator 反模式。它会起作用,但你会失去 IoC 的灵活性,并在各处添加难以测试的依赖项。
这个简单的答案是您可以将“KernelContainer.Inject(this)”添加到您的 HttpHandler 中。或者,您可以创建一个自定义模块(或修改现有的 Ninject.Web 模块)以在处理程序执行之前进行注入。
This is using the Service-Locator anti-pattern. It will work, but you lose the flexibility of IoC and add a dependency everywhere that is difficult to test.
This simple answer is that you can add "KernelContainer.Inject(this)" to your HttpHandler. Or you can create a custom module (or modify the existing Ninject.Web one) to do injection before handler execution.