加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将Ninject 2.0与ASP .Net 3.5一起使用

发布时间:2020-12-16 01:34:31 所属栏目:百科 来源:网络整理
导读:我正在尝试将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 IKer
我正在尝试将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

正确.

即使我没有尝试注册Logger,我也不理解,似乎它正试图创建它自己的.我该如何解决这个错误?我是否必须使用任何Ninject的扩展记录器?

谢谢
GK

解决方法

错误表明在某处,Ninject正在尝试将ILoggerFactory接口解析为具体类.根据您上面提到的参考资料,听起来您是ASP.Net网络应用程序是基于WebForms而不是ASP.Net MVC应用程序.

考虑到这一点,您的页面应该从PageBase派生,PageBase是Ninject Web库提供的抽象类.该类具有以下属性定义:

[Inject]
public ILogger Logger { get; set; }

因此,当您的页面被实例化时,Ninject会尝试将记录器注入页面上的属性.正如错误所暗示的那样,您需要为ILoggerFactory和ILogger定义绑定;但是,您提供的唯一绑定是IDataAccess.您还需要加载日志记录扩展库中定义的其中一个模块.我相信你可以选择NLog和Log4Net.因此,例如,如果您想使用Log4Net,您的CreateKernel函数将如下所示:

protected override IKernel CreateKernel()
{
    IKernel kernel = new StandardKernel(new Log4netModule());            
    kernel.Bind<IDataAccess>().To<DataAccessEntBlock>().InSingletonScope();
    return kernel;
}

这假设你有一个使用Ninject.Extensions.Logging.Log4net;文件中的声明.

在任何一种情况下,您都必须选择一个记录器并加载它的绑定(通过模块),因为Ninject Web库需要它.如果您现在没有任何日志记录问题,您可以选择提供不执行任何操作的ILoggerFactory和ILogger的实现(即“null”记录器)并自行绑定您的虚拟ILoggerFactory.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读