Click or drag to resize

ServiceContainer Class

This class combines the capabilities of a IServiceCollection and IServiceProvider into a single object that implements the combined IServiceContainer interface.
Inheritance Hierarchy

Namespace:  Neon.Common
Assembly:  Neon.Common (in Neon.Common.dll) Version: 2.14.0
public class ServiceContainer : IServiceContainer, 
	IServiceCollection, IList<ServiceDescriptor>, ICollection<ServiceDescriptor>, 
	IEnumerable<ServiceDescriptor>, IEnumerable, IServiceProvider

The ServiceContainer type exposes the following members.

Public methodServiceContainer
Public methodAdd
Public methodClear
Public methodContains
Public methodCopyTo
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetEnumerator
Returns an enumerator that iterates through the collection.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetService(Type)
Gets the service object of the specified type.
Public methodGetServiceTService
This is a convenience generic method that returns the service already cast to requested type. This only works for reference types.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodIndexOf
Public methodInsert
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodRemove
Public methodRemoveAt
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Extension Methods

The design of IServiceCollection and IServiceProvider seems somewhat limited. This assumes that applications explicitly initialize a IServiceCollection instance during startup and then call BuildServiceProvider() to return the IServiceProvider that can actually be used to find a service at runtime.

This works fine for lots of applications, but with a framework like Neon, it is useful to have a global service provider that allows the client to register default services for applications that are not coded to be aware of dependency injection. The problem with the Microsoft DependencyInjection design is that additional services registered after a BuildServiceProvider() call will not be returned by the service provider.

This class combines both these capabilities into a single class such that services can be registered and located dynamically without ever having to call BuildServiceProvider().

Note Note
The BuildServiceProvider() extension methods still work the same and return only a point-in-time snapshot of the services. You may not need to call these though, because you can call GetService(Type) directly.
Thread Safety
Instance members of this type are safe for multi-threaded operations.
See Also