Ninject的AllowNullInjection在ASP.NET WCF中不起作用

时间:2015-06-15 08:01:23

标签: c# asp.net wcf ninject

我在ASP.NET WCF项目中使用以下Ninject的NuGet包:

  • Ninject
  • Ninject.Web.Common
  • Ninject.Web.Common.WebHost
  • Ninject.Extensions.Wcf

我的WCF服务FooService取决于IRepository,具有以下具体实现FooRepository

[ServiceContract]
public interface IFooService
{
    [OperationContract]
    IEnumerable<Foo> GetFoos();
}

public class FooService : IFooService
{
    private readonly IRepository _repository;

    public FooService(IRepository repository)
    {
        _repository = repository;
    }
}

public class FooRepository : IRepository
{
    private readonly FooContext _fooContext;
    private readonly ILogger _logger;

    public FooRepository(FooContext fooContext, ILogger logger)
    {
        _fooContext = fooContext;
        _logger = logger;
    }
}

FooService.svc

<%@ ServiceHost Language="C#" 
                Debug="true" 
                Service="FooNamespace.FooService" 
                CodeBehind="FooService.svc.cs" 
                Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory" %>

我在NinjectWebCommon.cs中设置我的依赖项,如下所示:

private static void RegisterServices(IKernel kernel)
{
    kernel.Settings.AllowNullInjection = true;
    kernel.Bind<IRepository>().To<FooRepository>().InRequestScope();

    // I want to bind null to ILogger through the Settings property
    // instead of manually calling Bind<ILogger>().ToConstant<ILogger>(null)
}       

但是当我的WCF服务被激活时,我仍然得到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 FooRepository
 1) Injection of dependency IRepository into parameter repository of constructor of type FooService

在这种情况下如何设置可选注射?

0 个答案:

没有答案