views:

453

answers:

2

Hi,

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

+4  A: 

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:

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

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;
}

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

Peter Meyer
perfect. Thanks
GK
A: 

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.

prabir