AddSingleton
Service registration methods
See Service registration methods in Dependency injection in .NET
It's common to use multiple implementations when mocking types for testing.
Registering a service with only an implementation type is equivalent to registering that service with the same implementation and service type. This is why multiple implementations of a service cannot be registered using the methods that don't take an explicit service type. These methods can register multiple instances of a service, but they will all have the same implementation type.
Any of the above service registration methods can be used to register multiple service instances of the same service type. In the following example, AddSingleton
is called twice with IMyDependency
as the service type. The second call to AddSingleton
overrides the previous one when resolved as IMyDependency
and adds to the previous one when multiple services are resolved via IEnumerable<IMyDependency>
. Services appear in the order they were registered when resolved via IEnumerable<{SERVICE}>
.
Last updated