我有一个类的构造函数具有一个默认值的参数.使用Ninject 2.2,它将遵守[可选]属性并且正常工作,没有对声明为如下的构造函数定义绑定:
public EmployeeValidator([Optional] IValidator<PersonName> personNameValidator = null)
由于升级到Ninject 3.0,此对象的构造失败,并显示一条消息,指出提供程序返回null:
Test method ValidatorIsolated.Tests.EmployeeValidatorTest.CreateEmployeeValidatorTest threw exception:
Ninject.ActivationException: Error activating IValidator{PersonName} using conditional implicit self-binding of IValidator{PersonName}
Provider returned null.
Activation path:
2) Injection of dependency IValidator{PersonName} into parameter personNameValidator of constructor of type EmployeeValidator
1) Request for IValidator{Employee}
Suggestions:
1) Ensure that the provider handles creation requests properly.
解决方法
在这种情况下,可选属性被忽略,因为始终有默认值可用 – 但提供的值为null.默认情况下,Null不是允许的值.
您可以通过将NinjectSettings.AllowNullInjection设置为true来覆盖此行为.