ServiceLocatorImplBase seems to break the contract of IServiceProvider

Jul 27, 2012 at 12:08 PM
Edited Jul 27, 2012 at 12:08 PM

IServiceLocator extends the BCL interface IServiceProvider. The IServiceProvider.GetService (Type) method is very similar to IServiceLocator.GetInstance (Type), therefore, ServiceLocatorImplBase's implementation of GetService simply delegates to GetInstance.

However, GetInstance is documented to never return null (it must throw an ActivationException). GetService, on the other hand, is documented to return null if there is no service object (no exceptions are documented).

Therefore, it appears that ServiceLocatorImplBase, and with it probably nearly all implementations of IServiceLocator, break the IServiceProvider contract.

Has this been discussed before? Can it still be fixed?

Jul 30, 2012 at 3:15 PM

This has been discussed before here, and this design flaw has been confirmed by Glenn Block, the coordinator of the CSL.

Fixing this is a big breaking change, but not changing this IMO is not really an option, since the current implementation makes the abstraction unusable. For this reason the CSL Adapter for the Simple Injector does not inherit form the ServiceLocatorImplBase. Although the behavior of the Simple Injector adapter correctly implements the IServiceProvider contract, it is still unusable, simply because all other implementations break the contract.