GetService() - non explicit implementation of IServiceProvider

Developer
Oct 13, 2013 at 11:22 PM
I had a few situations in which I wanted the GetService() instead the GetInstance<>() behavior (e.g. return null if type is not supported) and I had to write something like:
((IServiceProvider)container).GetService(serviceType)
Why was the IServiceProvider interface explicitly implemented? I fear some users might also require the GetService() behavior but do not directly discover this option since it is not directly visible in intelli-sense.
Coordinator
Oct 14, 2013 at 6:38 AM
Edited Oct 14, 2013 at 8:28 PM
The GetService method is mainly needed for integration scenarios. Although important to have, I didn't want it to clutter the main API. I didn't want developers to call it, because in most cases you should call GetInstance and I didn't find the name GetService very expressive. Still, for compatibility, it is important for the Container to implement IServiceProvider. ThThat's why it's implemented explicitly. A better method name would be GetInstanceOrNull or TryGetInstance, but again, I wanted to add this to he main API. This was a compromise.
Marked as answer by dot_NET_Junkie on 3/2/2014 at 11:12 AM