Using default parameter values with Ninject 3.0

The Optional Attribute is ignored in this situation because there is always the default value available- But the provided value is null. Null is not an allowed value by default.

You can override this behavior by setting NinjectSettings.AllowNullInjection to true.


You can avoid setting AllowNullInjection globally while still injecting a parameter only when a binding exists by configuring your binding like this:

Kernel.Bind<IEmployeeValidator>()
      .To<EmployeeValidator>()
      .WithConstructorArgument(typeof(IValidator<PersonName>), c => c.Kernel.TryGet<IValidator<PersonName>>());

[Optional] is not needed for that.