将 Ninject 2.0 与 ASP .Net 3.5 结合使用
我正在尝试将 Ninject 2.0 与 Asp .Net 3.5 Web 应用程序一起使用。以下是 DLLS 及其我正在使用的版本。
- Ninject.dll - v2.0.0.0
- Ninject.Extensions.Logging.dll v2.0.0.0
- Ninject.Web.dll v1.0.0.0
在我的 global.ascx.cs 中,我有以下方法。
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel();
kernel.Bind<IDataAccess>().To<DataAccessEntBlock>().InSingletonScope();
return kernel;
}
当我运行该应用程序时,出现以下错误。
Error activating ILoggerFactory
No matching bindings are available, and the type is not self-bindable.
Activation path:
1) Request for ILoggerFactory
Suggestions:
1) Ensure that you have defined a binding for ILoggerFactory.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using automatic module loading, ensure the search path and filters are
正确的。
我不明白,即使我没有尝试注册记录器,它似乎正在尝试创建自己的记录器。我该如何解决这个错误?我必须使用 Ninject 的扩展 Logger 吗?
谢谢 吉克
I am trying to use Ninject 2.0 with Asp .Net 3.5 web application. Following are the DLLS and it's versions I am using.
- Ninject.dll - v2.0.0.0
- Ninject.Extensions.Logging.dll v2.0.0.0
- Ninject.Web.dll v1.0.0.0
In my global.ascx.cs I have following method.
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel();
kernel.Bind<IDataAccess>().To<DataAccessEntBlock>().InSingletonScope();
return kernel;
}
When I run the application I get following error.
Error activating ILoggerFactory
No matching bindings are available, and the type is not self-bindable.
Activation path:
1) Request for ILoggerFactory
Suggestions:
1) Ensure that you have defined a binding for ILoggerFactory.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using automatic module loading, ensure the search path and filters are
correct.
I am not understanding even though I am not trying to register Logger, it seems it is trying to create it's own. How can I resolve this error ? Do I have to use any of the Ninject's extension Logger ?
Thanks
GK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该错误表明 Ninject 正在尝试将 ILoggerFactory 接口解析为具体类。根据您上面提到的参考内容,听起来您的 ASP.Net Web 应用程序是基于 WebForms 的,而不是 ASP.Net MVC 应用程序。
考虑到这一点,您的页面应该从
PageBase
派生,它是 Ninject Web 库提供的抽象类。该类具有以下属性定义:因此,当实例化页面时,Ninject 会尝试将记录器注入页面上的属性中。正如错误所暗示的那样,您需要为
ILoggerFactory
以及ILogger
定义绑定;但是,您提供的唯一绑定是针对IDataAccess
的。您还需要加载日志扩展库中定义的模块之一。我相信你可以在 NLog 和 Log4Net 之间进行选择。因此,举例来说,如果您想使用 Log4Net,您的 CreateKernel 函数将如下所示:假设您有一个 using Ninject.Extensions.Logging.Log4net; 语句文件。
无论哪种情况,您都必须选择一个记录器并加载它的绑定(通过模块),因为 Ninject Web 库需要它。如果您现在没有任何日志记录问题,您可以选择提供不执行任何操作的
ILoggerFactory
和ILogger
实现(即“空”记录器)并绑定你自己的虚拟 ILoggerFactory 。The error indicates that somewhere, Ninject is trying to resolve the
ILoggerFactory
interface to a concrete class. Based on what you've stated as your references above, it sounds like you're ASP.Net web app is WebForms-based rather than an ASP.Net MVC app.With that in mind, your pages should be derived from
PageBase
which is an abstract class provided by the Ninject web library. That class has the following property definition:Therefore, when your page is instantiated, Ninject is trying to inject a logger into the property on the page. As the error alludes to, you need a binding defined for the
ILoggerFactory
as well asILogger
; however, the only binding you've provided is forIDataAccess
. You need to load one of the modules defined in the logging extensions library as well. I believe you can choose between NLog and Log4Net. So, for example, if you want to use Log4Net, yourCreateKernel
function would look like this:This assumes you have a
using Ninject.Extensions.Logging.Log4net;
statement in the file.In either case, you've got to select a logger and load it's bindings (via the module) as the Ninject Web library requires it. If you don't have any logging concerns right now, you could opt to provide implementations of
ILoggerFactory
andILogger
that do nothing (i.e. a "null" logger) and bind the your dummy ILoggerFactory yourself.我也有同样的问题。
尝试再次引用 ddls log4net.dll、ninject.extensions.logging.dll、ninject.extensions.log4net.dd、ningject.web.dll 和 ninject.dll
这解决了我的问题。最有可能是由于旧的 log4net.dll。
i had the same problem too.
try referencing ddls again log4net.dll, ninject.extensions.logging.dll, ninject.extensions.log4net.dd, ningject.web.dll and ninject.dll
this solved my problem. most probably due to old log4net.dll.