object GetInstance(Type serviceType)

This method is used for retrieving a single instance. For containers that support keyed instances, this should return the "default" instance (see more on default below). This method delegates to the GetInstance overload that accepts key and passes in a null key.

Requirements:

TService GetInstance<TService>()

This method is used for retrieving a strongly typed single instance. It should behave similar to object GetInstance(Type serviceType)

Requirements:

object GetInstance(Type serviceType, string key)

This method is used for retrieving a keyed instance from the container.

Requirements:

TService GetInstance<TService>(string key)

This method is used for retrieving a strongly typed keyed instance. It should behave similar to object GetInstance(Type serviceType, string key)

Requirements:

IEnumerable<object> GetAllInstances(Type serviceType)

This method retrieves all the instances from the locator of the specificed type.

Requirements:

IEnumerable<TService> GetAllInstances<TService>()

This method retrives a strongly typed collection from the locator of the specified type.

Requirements

Overload Behavior

A call to:
    object IServiceLocator.GetInstance(serviceType)
MUST be exactly equivalent to a call to:
    object IServiceLocator.GetInstance(serviceType, null)
A call to:
    TService IServiceLocator.GetInstance<TService>()
MUST be exactly equivalent to a call to:
    (TService)IServiceLocator.GetInstance(typeof(TService), null)

A call to:
    TService IServiceLocator.GetInstance<TService>(key)
MUST be exactly equivalent to a call to:
    (TService)IServiceLocator.GetInstance(typeof(TService), key)
A call to:
    IEnumerable<TService> IServiceLocator.GetAllInstances<TService>()
Must be exactly equivalent to a call to:
    IEnumerable<object> IServiceLocator.GetAllInstances(typeof(TService))

with the exception that the objects returned by the enumerator are already cast to type TService.

Throwing ActivationException

ServiceLocatorImplBase

This class is not part of the specification; consumers should only reference the IServiceLocator interface. ServiceLocatorImplBase is provided as a convenience for implementors of IServiceLocator. It implements the correct overload semantics and exception wrapping behavior defined above. You just need to implement the two protected methods DoGetInstance and DoGetAllInstances and the rest will just work. In addition, the two protected methods FormatActivationExceptionMessage and FormatActivateAllExceptionMessage are provided if you wish to customize the error message reported in the exceptions.

Why is ActivationException a partial class?

Implementing ISerializable for exceptions is a .NET best practice for the desktop CLR. However, I anticipate a port to Silverlight for this code - many containers are already supporting Silverlight beta 2 or are about to. Silverlight does not support classic binary serialization. By making this a partial class and segregating the serialization details into a separate file, a future Silverlight port can simply leave the .Desktop.cs file out of the project and the incompatible code will be seamlessly removed.