Ninject - Binding.GetProvider throws NullReferenceException Ninject - Binding.GetProvider throws NullReferenceException asp.net asp.net

Ninject - Binding.GetProvider throws NullReferenceException


I can't tell you the reason for this issue as I can't reproduce such a behavior. But here are some steps you can take to identify the problem.

As you say the issue is caused by a ProviderCallback that is null. This can't be caused by GC because GC will never assign null to a property. Instead you will get an already disposed exception or other strange behaviors. But there are some other reasons how this can happen:

  1. Null is assigned at some time, but since you verified this already this is not the reason.
  2. It was never assigned at all.
  3. A new BindingConfiguration is created at a later time.

The 3rd point can easily be verified by adding a breakpoint in the BindingConfiguration constructor. It shouldn't be called anymore after the kernel is succesfully configured and you start resolving objects.

For the second problem execute the following after the kernel configuration:

var kernel = your fully configured kernel;var bindingsField = typeof(KernelBase).GetField("bindings", BindingFlags.NonPublic | BindingFlags.Instance);var bindings = bindingsField.GetValue(kernel) as IEnumerable<KeyValuePair<Type, ICollection<IBinding>>>;foreach (var bindingsEntry in bindings    .Where(bindingsEntry => bindingsEntry.Value        .Any(binding => binding.BindingConfiguration.ProviderCallback == null))){    throw new Exception(string.Format("No Provider callback defined for {0}.", bindingsEntry.Key));}