Container Class

Simple Injector
The container. Create an instance of this type for registration of dependencies.
Common Container methods specific for the full .NET version of Simple Injector.
Methods for batch registration.
Methods for registration of collections.
Methods for conditional registrations.
Methods for registration.
Methods for registration of decorators.
Methods for resolving instances.
Methods for verifying the container.
Inheritance Hierarchy

SystemObject
  SimpleInjectorContainer

Namespace:  SimpleInjector
Assembly:  SimpleInjector (in SimpleInjector.dll) Version: 4.0.0
Syntax

public class Container : IDisposable, IServiceProvider

The Container type exposes the following members.

Constructors

  NameDescription
Public methodContainer
Initializes a new instance of the Container class.
Top
Properties

  NameDescription
Public propertyIsVerifying
Gets a value indicating whether the container is currently being verified on the current thread.
Public propertyOptions
Gets the container options.
Top
Methods

  NameDescription
Public methodCode exampleAddRegistration(Type, Registration)
Adds the registration for the supplied serviceType. This method can be used to apply the same Registration to multiple different service types.
Public methodAddRegistrationTService(Registration)
Adds the registration for the supplied . This method can be used to apply the same Registration to multiple different service types.
Public methodDispose
Releases all instances that are cached by the Container object.
Protected methodDispose(Boolean)
Releases all instances that are cached by the Container object.
Public methodEquals
Determines whether the specified System.Object is equal to the current System.Object.
(Overrides ObjectEquals(Object).)
Public methodGetAllInstances(Type)
Gets all instances of the given serviceType currently registered in the container.
Public methodGetAllInstancesTService
Gets all instances of the given TService currently registered in the container.
Public methodGetCurrentRegistrations
Returns an array with the current registrations. This list contains all explicitly registered types, and all implicitly registered instances. Implicit registrations are all concrete unregistered types that have been requested, all types that have been resolved using unregistered type resolution (using the ResolveUnregisteredType event), and requested unregistered collections. Note that the result of this method may change over time, because of these implicit registrations.
Public methodGetHashCode
Returns the hash code of the current instance.
(Overrides ObjectGetHashCode.)
Public methodGetInstance(Type)
Gets an instance of the given serviceType.
Public methodGetInstanceTService
Gets an instance of the given TService.
Public methodGetRegistration(Type)
Gets the InstanceProducer for the given serviceType. When no registration exists, the container will try creating a new producer. A producer can be created when the type is a concrete reference type, there is an ResolveUnregisteredType event registered that acts on that type, or when the service type is an IEnumerableT. Otherwise null (Nothing in VB) is returned.
Public methodGetRegistration(Type, Boolean)
Gets the InstanceProducer for the given serviceType. When no registration exists, the container will try creating a new producer. A producer can be created when the type is a concrete reference type, there is an ResolveUnregisteredType event registered that acts on that type, or when the service type is an IEnumerableT. Otherwise null (Nothing in VB) is returned, or an exception is throw when throwOnFailure is set to true.
Public methodGetRootRegistrations
Returns an array with the current registrations for root objects. Root objects are registrations that are in the root of the object graph, meaning that no other registration is depending on it.
Public methodGetType
Gets the Type of the current instance.
Public methodGetTypesToRegister(Type, IEnumerableAssembly)
Returns all concrete non-generic types that are located in the supplied assemblies and implement or inherit from the supplied serviceType. serviceType can be an open-generic type.
Public methodGetTypesToRegister(Type, IEnumerableAssembly, TypesToRegisterOptions)
Returns all concrete types that are located in the supplied assemblies and implement or inherit from the supplied serviceType and match the specified options... serviceType can be an open-generic type.
Public methodRegister(Type)
Registers that a new instance of concreteType will be returned every time it is requested (transient).
Public methodRegister(Type, IEnumerableAssembly)
Registers all concrete, non-generic, public and internal types in the given set of assemblies that implement the given openGenericServiceType with container's default lifestyle (which is transient by default). Decorators and generic type definitions will be excluded from registration, while composites are included.
Public methodRegister(Type, IEnumerableType)
Registers all supplied implementationTypes based on the closed-generic version of the given openGenericServiceType with the transient lifestyle.
Public methodRegister(Type, FuncObject)
Registers the specified delegate that allows returning instances of serviceType.
Public methodRegister(Type, Type)
Registers that a new instance of implementationType will be returned every time a serviceType is requested. If serviceType and implementationType represent the same type, the type is registered by itself. Open and closed generic types are supported.
Public methodRegister(Type, IEnumerableAssembly, Lifestyle)
Registers all concrete, non-generic, public and internal types in the given set of assemblies that implement the given openGenericServiceType with the supplied lifestyle. Decorators and generic type definitions will be excluded from registration, while composites are included.
Public methodRegister(Type, IEnumerableType, Lifestyle)
Registers all supplied implementationTypes based on the closed-generic version of the given openGenericServiceType with the given lifestyle.
Public methodRegister(Type, FuncObject, Lifestyle)
Registers the specified delegate instanceCreator that will produce instances of type serviceType and will be returned when an instance of type serviceType is requested. The delegate is expected to produce new instances on each call. The instances are cached according to the supplied lifestyle.
Public methodRegister(Type, Type, Lifestyle)
Registers that an instance of type implementationType will be returned when an instance of type serviceType is requested. The instance is cached according to the supplied lifestyle. Open and closed generic types are supported.
Public methodRegisterTConcrete
Registers that a new instance of TConcrete will be returned every time it is requested (transient).
Public methodRegisterTService(FuncTService)
Registers the specified delegate that allows returning transient instances of TService. The delegate is expected to always return a new instance on each call.
Public methodRegisterTConcrete(Lifestyle)
Registers that an instance of TConcrete will be returned when it is requested. The instance is cached according to the supplied lifestyle.
Public methodRegisterTService(FuncTService, Lifestyle)
Registers the specified delegate instanceCreator that will produce instances of type TService and will be returned when an instance of type TService is requested. The delegate is expected to produce new instances on each call. The instances are cached according to the supplied lifestyle.
Public methodRegisterTService, TImplementation
Registers that a new instance of TImplementation will be returned every time a TService is requested (transient).
Public methodRegisterTService, TImplementation(Lifestyle)
Registers that an instance of TImplementation will be returned when an instance of type TService is requested. The instance is cached according to the supplied lifestyle.
Public methodRegisterCollection(Type, IEnumerableRegistration)
Registers a collection of registrations, whose instances will be resolved lazily each time the resolved collection of serviceType is enumerated. The underlying collection is a stream that will return individual instances based on their specific registered lifestyle, for each call to Current. The order in which the types appear in the collection is the exact same order that the items were registered, i.e the resolved collection is deterministic.
Public methodRegisterCollection(Type, IEnumerableAssembly)
Registers all concrete, non-generic types (both public and internal) that are defined in the given set of assemblies and that implement the given serviceType with a default lifestyle and register them as a collection of serviceType. Unless overridden using a custom LifestyleSelectionBehavior, the default lifestyle is Transient. Composites, decorators and generic type definitions will be excluded from registration.
Public methodRegisterCollection(Type, IEnumerableType)
Registers a collection of serviceTypes, whose instances will be resolved lazily each time the resolved collection of serviceType is enumerated. The underlying collection is a stream that will return individual instances based on their specific registered lifestyle, for each call to Current. The order in which the types appear in the collection is the exact same order that the items were registered, i.e the resolved collection is deterministic.
Public methodRegisterCollection(Type, IEnumerable)
Registers a dynamic (container uncontrolled) collection of elements of type serviceType. A call to GetAllInstancesTService will return the containerUncontrolledCollection itself, and updates to the collection will be reflected in the result. If updates are allowed, make sure the collection can be iterated safely if you're running a multi-threaded application.
Public methodRegisterCollection(Type, Assembly)
Registers all concrete, non-generic types (both public and internal) that are defined in the given set of assemblies and that implement the given serviceType with a default lifestyle and register them as a collection of serviceType. Unless overridden using a custom LifestyleSelectionBehavior, the default lifestyle is Transient. Composites, decorators and generic type definitions will be excluded from registration.
Public methodRegisterCollectionTService(IEnumerableAssembly)
Registers all concrete, non-generic types (both public and internal) that are defined in the given set of assemblies and that implement the given TService with a default lifestyle and register them as a collection of TService. Unless overridden using a custom LifestyleSelectionBehavior, the default lifestyle is Transient.
Public methodRegisterCollectionTService(IEnumerableType)
Registers a collection of serviceTypes, whose instances will be resolved lazily each time the resolved collection of TService is enumerated. The underlying collection is a stream that will return individual instances based on their specific registered lifestyle, for each call to Current. The order in which the types appear in the collection is the exact same order that the items were registered, i.e the resolved collection is deterministic.
Public methodRegisterCollectionTService(IEnumerableTService)
Registers a dynamic (container uncontrolled) collection of elements of type TService. A call to GetAllInstancesTService will return the containerUncontrolledCollection itself, and updates to the collection will be reflected in the result. If updates are allowed, make sure the collection can be iterated safely if you're running a multi-threaded application.
Public methodRegisterCollectionTService(TService)
Registers a collection of singleton elements of type TService.
Public methodRegisterCollectionTService(IEnumerableRegistration)
Registers a collection of registrations, whose instances will be resolved lazily each time the resolved collection of TService is enumerated. The underlying collection is a stream that will return individual instances based on their specific registered lifestyle, for each call to Current. The order in which the types appear in the collection is the exact same order that the items were registered, i.e the resolved collection is deterministic.
Public methodRegisterConditional(Type, Registration, PredicatePredicateContext)
Conditionally registers that registration will be used every time a serviceType is requested and where the supplied predicate returns true. The predicate will only be evaluated a finite number of times; the predicate is unsuited for making decisions based on runtime conditions.
Public methodRegisterConditional(Type, Type, PredicatePredicateContext)
Conditionally registers that a new instance of implementationType will be returned every time a serviceType is requested (transient) and where the supplied predicate returns true. The predicate will only be evaluated a finite number of times; the predicate is unsuited for making decisions based on runtime conditions.
Public methodRegisterConditional(Type, FuncTypeFactoryContext, Type, Lifestyle, PredicatePredicateContext)
Conditionally registers that an instance of the type returned from implementationTypeFactory will be returned every time a serviceType is requested and where the supplied predicate returns true. The instance is cached according to the supplied lifestyle. Both the predicate and implementationTypeFactory will only be evaluated a finite number of times; they unsuited for making decisions based on runtime conditions.
Public methodRegisterConditional(Type, Type, Lifestyle, PredicatePredicateContext)
Conditionally registers that an instance of implementationType will be returned every time a serviceType is requested and where the supplied predicate returns true. The instance is cached according to the supplied lifestyle. The predicate will only be evaluated a finite number of times; the predicate is unsuited for making decisions based on runtime conditions.
Public methodRegisterConditionalTService, TImplementation(PredicatePredicateContext)
Conditionally registers that a new instance of TImplementation will be returned every time a TService is requested (transient) and where the supplied predicate returns true. The predicate will only be evaluated a finite number of times; the predicate is unsuited for making decisions based on runtime conditions.
Public methodRegisterConditionalTService, TImplementation(Lifestyle, PredicatePredicateContext)
Conditionally registers that an instance of TImplementation will be returned every time a TService is requested and where the supplied predicate returns true. The instance is cached according to the supplied lifestyle. The predicate will only be evaluated a finite number of times; the predicate is unsuited for making decisions based on runtime conditions.
Public methodCode exampleRegisterDecorator(Type, Type)
Ensures that the supplied decoratorType decorator is returned, wrapping the original registered serviceType, by injecting that service type into the constructor of the supplied decoratorType. Multiple decorators may be applied to the same serviceType. Decorators can be applied to both open, closed, and non-generic service types. By default, a new decoratorType instance will be returned on each request (according the Transient lifestyle), independently of the lifestyle of the wrapped service.
Public methodCode exampleRegisterDecorator(Type, Type, Lifestyle)
Ensures that the supplied decoratorType decorator is returned and cached with the given lifestyle, wrapping the original registered serviceType, by injecting that service type into the constructor of the supplied decoratorType. Multiple decorators may be applied to the same serviceType. Decorators can be applied to both open, closed, and non-generic service types.
Public methodCode exampleRegisterDecorator(Type, Type, PredicateDecoratorPredicateContext)
Ensures that the supplied decoratorType decorator is returned when the supplied predicate returns true, wrapping the original registered serviceType, by injecting that service type into the constructor of the supplied decoratorType. Multiple decorators may be applied to the same serviceType. Decorators can be applied to both open, closed, and non-generic service types. By default, a new decoratorType instance will be returned on each request (according the Transient lifestyle), independently of the lifestyle of the wrapped service.
Public methodCode exampleRegisterDecorator(Type, FuncDecoratorPredicateContext, Type, Lifestyle, PredicateDecoratorPredicateContext)
Ensures that the decorator type that is returned from decoratorTypeFactory is supplied when the supplied predicate returns true and cached with the given lifestyle, wrapping the original registered serviceType, by injecting that service type into the constructor of the decorator type that is returned by the supplied decoratorTypeFactory. Multiple decorators may be applied to the same serviceType. Decorators can be applied to both open, closed, and non-generic service types.
Public methodCode exampleRegisterDecorator(Type, Type, Lifestyle, PredicateDecoratorPredicateContext)
Ensures that the supplied decoratorType decorator is returned when the supplied predicate returns true and cached with the given lifestyle, wrapping the original registered serviceType, by injecting that service type into the constructor of the supplied decoratorType. Multiple decorators may be applied to the same serviceType. Decorators can be applied to both open, closed, and non-generic service types.
Public methodCode exampleRegisterDecoratorTService, TDecorator
Ensures that the supplied TDecorator decorator is returned, wrapping the original registered TService, by injecting that service type into the constructor of the supplied TDecorator. Multiple decorators may be applied to the same TService. By default, a new TDecorator instance will be returned on each request (according the Transient lifestyle), independently of the lifestyle of the wrapped service.
Public methodCode exampleRegisterDecoratorTService, TDecorator(Lifestyle)
Ensures that the supplied TDecorator decorator is returned and cached with the given lifestyle, wrapping the original registered TService, by injecting that service type into the constructor of the supplied TDecorator. Multiple decorators may be applied to the same TService. Decorators can be applied to both open, closed, and non-generic service types.
Public methodRegisterInitializer(ActionInstanceInitializationData, PredicateInitializerContext)
Registers an ActionT delegate that runs after the creation of instances for which the supplied predicate returns true. Please note that only instances that are created by the container can be initialized this way.
Public methodRegisterInitializerTService(ActionTService)
Registers an ActionT delegate that runs after the creation of instances that implement or derive from the given TService. Please note that only instances that are created by the container (using constructor injection) can be initialized this way.
Public methodRegisterSingleton(Type, FuncObject)
Registers the specified delegate that allows constructing a single serviceType instance. The container will call this delegate at most once during the lifetime of the application.
Public methodRegisterSingleton(Type, Object)
Registers a single instance that will be returned when an instance of type serviceType is requested. This instance must be thread-safe when working in a multi-threaded environment.
Public methodRegisterSingleton(Type, Type)
Registers that the same instance of type implementationType will be returned every time an instance of type serviceType type is requested. If serviceType and implementationType represent the same type, the type is registered by itself. implementationType must be thread-safe when working in a multi-threaded environment. Open and closed generic types are supported.
Public methodRegisterSingletonTConcrete
Registers a single concrete instance that will be constructed using constructor injection and will be returned when this instance is requested by type TConcrete. This TConcrete must be thread-safe when working in a multi-threaded environment. If TConcrete implements IDisposable, a created instance will get disposed when Container.Dispose gets called.
Public methodRegisterSingletonTService(FuncTService)
Registers the specified delegate that allows constructing a single instance of TService. This delegate will be called at most once during the lifetime of the application. The returned instance must be thread-safe when working in a multi-threaded environment. If the instance returned from instanceCreator implements IDisposable, the created instance will get disposed when Container.Dispose gets called.
Public methodRegisterSingletonTService(TService)
Registers a single instance that will be returned when an instance of type TService is requested. This instance must be thread-safe when working in a multi-threaded environment. NOTE: Do note that instances supplied by this method NEVER get disposed by the container, since the instance is assumed to outlive this container instance. If disposing is required, use the overload that accepts a FuncTResult delegate.
Public methodRegisterSingletonTService, TImplementation
Registers that the same a single instance of type TImplementation will be returned every time an TService type is requested. If TService and TImplementation represent the same type, the type is registered by itself. TImplementation must be thread-safe when working in a multi-threaded environment. If TImplementation implements IDisposable, a created instance will get disposed when Container.Dispose gets called.
Public methodToString
Returns a String that represents the Container.
(Overrides ObjectToString.)
Public methodVerify
Verifies and diagnoses this Container instance. This method will call all registered delegates, iterate registered collections and throws an exception if there was an error.
Public methodVerify(VerificationOption)
Verifies the Container. This method will call all registered delegates, iterate registered collections and throws an exception if there was an error.
Top
Events

  NameDescription
Public eventCode exampleExpressionBuilding
Occurs directly after the creation of the Expression of a registered type is made, but before any initializer and lifestyle specific caching has been applied, allowing the created Expression to be altered. Multiple delegates may handle the same service type.
Public eventCode exampleExpressionBuilt
Occurs after the creation of the Expression of a registered type is complete (the lifestyle has been applied), allowing the created Expression to be wrapped, changed, or replaced. Multiple delegates may handle the same service type.
Public eventCode exampleResolveUnregisteredType
Occurs when an instance of a type is requested that has not been registered explicitly, allowing resolution of unregistered types before the container tries to create the type.
Top
Extension Methods

  NameDescription
Public Extension MethodAppendToCollection(Type, Registration)Overloaded.
Allows appending new registrations to existing registrations made using one of the RegisterCollection overloads.
(Defined by AdvancedExtensions.)
Public Extension MethodAppendToCollection(Type, Type)Overloaded.
Allows appending new registrations to existing registrations made using one of the RegisterCollection overloads.
(Defined by AdvancedExtensions.)
Public Extension MethodEnableHttpRequestMessageTracking
Makes the current HttpRequestMessage resolvable by calling GetCurrentHttpRequestMessage.
(Defined by SimpleInjectorWebApiExtensions.)
Public Extension MethodGetCurrentHttpRequestMessage
Retrieves the HttpRequestMessage instance for the current request.
(Defined by SimpleInjectorWebApiExtensions.)
Public Extension MethodGetItem
Retrieves an item from the container stored by the given key or null when no item is stored by that key.
(Defined by AdvancedExtensions.)
Public Extension MethodGetOrSetItemT
Adds an item by the given key in the container by using the specified function, if the key does not already exist. This operation is atomic.
(Defined by AdvancedExtensions.)
Public Extension MethodGetPackagesToRegister
Loads all IPackage implementations from the given set of assemblies and returns a list of created package instances.
(Defined by PackageExtensions.)
Public Extension MethodIsLocked
Determines whether the specified container is locked making any new registrations. The container is automatically locked when GetInstance is called for the first time.
(Defined by AdvancedExtensions.)
Public Extension MethodIsVerifying
Determines whether the specified container is currently verifying its configuration.
(Defined by AdvancedExtensions.)
Public Extension MethodRegisterMvcControllers
Registers the MVC IController instances (which name end with 'Controller') that are declared as public non-abstract in the supplied set of assemblies.
(Defined by SimpleInjectorMvcExtensions.)
Public Extension MethodRegisterMvcIntegratedFilterProvider
Registers a IFilterProvider that allows filter attributes to go through the Simple Injector pipeline (https://simpleinjector.org/pipel). This allows any registered property to be injected if a custom IPropertySelectionBehavior in configured in the container, and allows anyinitializers to be called on those attributes. Please note that attributes are cached by MVC, so only dependencies should be injected that have the singleton lifestyle.
(Defined by SimpleInjectorMvcExtensions.)
Public Extension MethodRegisterPackagesOverloaded.
Loads all IPackage implementations from assemblies that are currently loaded in the current AppDomain, and calls their Register method. Note that only publicly exposed classes that contain a public default constructor will be loaded. Note that this method will only pick up assemblies that are loaded at that moment in time. A more reliable way of registering packages is by explicitly supplying the list of assemblies using the RegisterPackages(Container, IEnumerableAssembly) overload.
(Defined by PackageExtensions.)
Public Extension MethodRegisterPackages(IEnumerableAssembly)Overloaded.
Loads all IPackage implementations from the given set of assemblies and calls their Register method. Note that only publicly exposed classes that contain a public default constructor will be loaded.
(Defined by PackageExtensions.)
Public Extension MethodRegisterWcfServices
Registers the WCF services instances (public classes that implement an interface that is decorated with a ServiceContractAttribute) that are declared as public non-abstract in the supplied set of assemblies.
(Defined by SimpleInjectorWcfExtensions.)
Public Extension MethodRegisterWebApiControllers(HttpConfiguration)Overloaded.
Registers the Web API IHttpController types that available for the application. This method uses the configured IAssembliesResolver and IHttpControllerTypeResolver to determine which controller types to register.
(Defined by SimpleInjectorWebApiExtensions.)
Public Extension MethodRegisterWebApiControllers(HttpConfiguration, Assembly)Overloaded.
Registers the Web API IHttpController types that available for the application. This method uses the configured IHttpControllerTypeResolver to determine which controller types to register.
(Defined by SimpleInjectorWebApiExtensions.)
Public Extension MethodRegisterWebApiControllers(HttpConfiguration, IEnumerableAssembly)Overloaded.
Registers the Web API IHttpController types that available for the application. This method uses the configured IHttpControllerTypeResolver to determine which controller types to register.
(Defined by SimpleInjectorWebApiExtensions.)
Public Extension MethodSetItem
Stores an item by the given key in the container.
(Defined by AdvancedExtensions.)
Top
Explicit Interface Implementations

  NameDescription
Explicit interface implementationPrivate methodIServiceProviderGetService
Gets the service object of the specified type.
Top
Remarks

Thread-safety: Resolving instances can be done safely from multiple threads concurrently, but registration needs to be done from one single thread.

It is therefore safe to call GetInstance(Type), GetAllInstances(Type), GetService, GetRegistration(Type) and GetCurrentRegistrations and anything related to resolving instances from multiple thread concurrently. It is however unsafe to call RegisterXXX, ExpressionBuilding, ExpressionBuilt, ResolveUnregisteredType, AddRegistration(Type, Registration) or anything related to registering from multiple threads concurrently.

See Also

Reference