使用Ninject 2.0的ASP.NET 3.5 [英] Using Ninject 2.0 with ASP .Net 3.5

查看:255
本文介绍了使用Ninject 2.0的ASP.NET 3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Ninject 2.0的ASP.NET 3.5 Web应用程序。 。以下是DLL和它是我使用的版本

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

在我global.ascx.cs我有以下的方法。

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.

我不理解,即使我没有尝试注册记录仪,它似乎它试图创建自己的。我怎样才能解决这个问题?我一定要使用任何Ninject的扩展日志记录器?

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 ?

由于
GK

Thanks GK

推荐答案

该错误指示的地方,Ninject是试图解决 ILoggerFactory 接口的具体类。根据你所描述为你上面的引用,这听起来像你ASP.Net Web应用程序是WebForms的基础,而不是一个ASP.Net MVC应用程序。

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.

考虑到这一点,你的页面应该从 PageBase 导出这是由Ninject网站提供了一个抽象类图书馆。那类具有以下属性定义:

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:

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



因此,当你的页面被实例化,Ninject试图注入一个记录器插入财产页。由于错误暗示,你需要一个绑定的定义 ILoggerFactory 以及 ILogger ;然而,只有结合您提供的是 IDataAccess 。你需要加载在日志扩展库中定义以及模块之一。我相信你可以NLOG和log4net的选择。因此,举例来说,如果你想使用log4net的,你的 CreateKernel 函数应该是这样的:

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 as ILogger; however, the only binding you've provided is for IDataAccess. 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, your CreateKernel function would look like this:

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

这假设你有一个使用Ninject.Extensions。 Logging.Log4net; 语句文件中的

This assumes you have a using Ninject.Extensions.Logging.Log4net; statement in the file.

在这两种情况下,你必须选择一个记录器并加载它的绑定(通过模块)作为Ninject网络库需要它。如果你没有任何记录的担忧,现在,你可以选择提供的实施 ILoggerFactory ILogger 的做什么(即一个空记录器),并结合您的虚拟ILoggerFactory自己。

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 and ILogger that do nothing (i.e. a "null" logger) and bind the your dummy ILoggerFactory yourself.

这篇关于使用Ninject 2.0的ASP.NET 3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆