I'm using version 2.2.0.0 of Ninject in a asp.net web forms application and after a few hundred requests, it sometimes throws a NullReferenceException in the GetProvider method of the Binding class.
Example stack trace: http://pastebin.com/BbhsPQMT
The exception only occurs when I stress test the application and the origin of the exception is usually different (resolving distinct interfaces).
In order to try to understand why this issue was ocurring I looked at the Ninject source code and inserted some lines of code for debugging purposes. I later confirmed that the object that is null is the ProviderCallback property in the Binding class.
I also put some code in the set operator of the ProviderCallback property in order to understand if it was being set to null. After running some tests and looking at some memory dumps it seems that the ProviderCallback property is not being set to a null value and so I think the instance being collected by GC.
I still do not understand why this is happening...
Any help is greatly appreciated.
Edit: We upgraded to the latest version of Ninject just to check if the exception still occurs but we got the same exception after stress testing the application: http://pastebin.com/YaiaZndz
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:
Null
is assigned at some time, but since you verified this already this is not the reason.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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With