ServiceCollection and IServiceProvider

Topics: CAB & Smart Client Software Factory
Jul 14, 2006 at 5:14 AM
originally posted by: JaredBienz

I was just wondering why ServiceCollection doesn't implement System.ComponentModel.IServiceContainer or at least System.ComponentModel.IServiceProvider? I can make a lightweight wrapper, but I was just wondering if there is a reason why those weren't implemented already.
Jul 14, 2006 at 7:23 AM
originally posted by: PProvost

What would you use it for?
Jul 14, 2006 at 5:09 PM
originally posted by: JaredBienz

The main thing I would want to use it for is to expose the services in ServiceCollection to a XAML MarkupExtension. Though I realize I could add the ServiceCollection as a service in the service provider, I was wondering why ServiceCollection was not an IServiceProvider or IServiceContainer itself. ServiceCollection already implements the same methods with the same exact method signatures, so I was just curious why they didn't bother to implement the interface.
Jul 14, 2006 at 5:13 PM
originally posted by: JaredBienz

P.S. I realize I am going to have difficulty integrating to a MarkupExtension and I am working on that. Regardless, System.ComponentModel.IServiceProvider is a well-known interface used in all sorts of places. UITypeEditors, the WinForms designer framework and the Workflow foundation are just a few examples.
Jul 15, 2006 at 5:42 AM
originally posted by: JaredBienz

Last night I created a wrapper for ServiceCollection that provides these interfaces. I realized during this exercise that some of my previous statements were not entirely accurate.

ServiceCollection can fully implement IServiceProvider, though it's current method signature is not identical. ServiceCollection has:

public object Get(Type serviceType)

whereas IServiceProvider has:

public object GetService(Type serviceType)

It's a similar story with IServiceContainer having Add instead of AddService and Remove instead of RemoveService. Of course, these names make perfect sense as we are dealing with a collection rather then a provider.

I also realized that ServiceCollection cannot fully implement IServiceContainer as it doesn't provide a ServiceCreatorCallback for delayed creation services. Therefore I was able to wrap the AddService methods that do not provide callbacks but had to throw a NotSupportedException for the ones that do.