ninject / Ninject

the ninja of .net dependency injectors
http://ninject.org/
Other
2.68k stars 530 forks source link

KernelBase is implementing IServiceProvider.GetService incorrectly #376

Closed lord-executor closed 2 years ago

lord-executor commented 4 years ago

According to the documentation of IServiceProvider, the method GetService is supposed to return null if there is no service object of the requested service type. KernelBase however delegates the call to the Get method which throws an exception in that case instead of delegating to the TryGet method which would be in line with the documentation.

This little quirk is the cause of some relatively weird and unnecessary workarounds when trying to integrate Ninject into a context like ASP.NET Core which mostly relies on the IServiceProvider interface. Effectively, one has to create a separate IServiceProvider implementation that wraps the kernel instead of just using the kernel directly.

As far as I can tell, that has been the expected behavior of the IServiceProvider from the very beginning and the Ninject implementation is just slightly off-spec.