3

I can't seem to get ninject to automatically create an instance of ILogger. I have log4net set up in my tests application and configured properly but for some reason when I instantiate the IHouseholdRepository it can't work out the ILogger.

according to this post, I have done everything i need to do: How To Properly Configure Ninject.Extensions.Logging.Log4Net in my MVC3 project

[TestMethod]
public void TestMethod1()
{
    var kernel = new StandardKernel();
    kernel.Bind<IHouseholdRepository>().To<HouseholdRepository>();

    var repo = kernel.Get<IHouseholdRepository>();
    var hh = repo.Find(123456);
}

public HouseholdRepository(ILogger logger, string username)
{
    _logger = logger;
    _username = username;
    _dbContext = new HouseholdDbContext(logger);  // I want to couple my HouseholdDBContect to my repository as it's not going to work with any other DbContext
}

The error I'm getting is:

Test method Rxxx.Tests.LoggingTester.TestMethod1 threw exception: Ninject.ActivationException: Error activating ILogger No matching bindings are available, and the type is not self-bindable. Activation path: 2) Injection of dependency ILogger into parameter logger of constructor of type HouseholdRepository 1) Request for IHouseholdRepository

Suggestions: 1) Ensure that you have defined a binding for ILogger. 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 constructor arguments, ensure that the parameter name matches the constructors parameter name. 5) If you are using automatic module loading, ensure the search path and filters are correct.

4

3 回答 3

7

我刚刚遇到了同样的问题,发现这是因为我忘记添加Ninject.Extensions.Logging.Log4net nuget 包。

于 2014-07-29T13:20:30.233 回答
1

我在sitecore 8项目中遇到了同样的问题,发现这是因为预建脚本没有将libs/Social文件夹中的ninject.dll复制到/bin文件夹中,所以每次我清理/bin文件夹我遇到了这个问题。预建脚本是在 libs 中没有子文件夹的时候构建的……</p>

于 2015-03-19T15:59:40.433 回答
0

我有同样的失败,但原因不同,虽然我觉得这个问题没有帮助,但我想我会添加我的答案。我使用的是 ReSharper 测试运行程序,默认情况下它为每个程序集使用单独的应用程序域。结果是 Ninject 内核被初始化为绑定所需的 Log4Net 扩展接口。

解决方法是更改​​ ReSharper 选项:ReSharper | 选项 | 工具 | 单元测试并检查“为每个程序集使用单独的 AppDomain”。这为我清除了它。我希望这对其他人有帮助!

于 2015-06-27T18:30:03.453 回答