ServiceCollectionExtensions Class
Provides extension methods for Microsoft.Extensions.DependencyInjection.IServiceCollection to enhance dependency injection capabilities.
public static class ServiceCollectionExtensionsInheritance System.Object → ServiceCollectionExtensions
Methods
ServiceCollectionExtensions.AddAlias<TService,TImplementation>(this IServiceCollection) Method
Creates an alias registration that allows resolving a service through a different interface or type.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddAlias<TService,TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services)
where TService : class
where TImplementation : class, TService;Type parameters
TService
The service type for the alias registration.
TImplementation
The implementation service type that is already registered.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
Exceptions
System.InvalidOperationException
Thrown when no registration is found for TImplementation.
Remarks
This method creates an alias by cloning the source service descriptor with a new service type. The alias preserves the lifetime of the source registration (Singleton, Scoped, or Transient).
For Singleton lifetime with factory-based registrations, ensures the same instance is returned when resolving through either the source type or the alias. For Scoped and Transient lifetimes, the alias resolves through the source service to maintain proper lifetime semantics.
Supports both interface-to-interface aliasing (e.g., AddAlias<IBase, IPrimary>())
and interface-to-implementation aliasing (e.g., AddAlias<IService, ServiceImpl>()).
When multiple different source services are aliased to the same target interface,
IEnumerable<TService> resolution returns instances from all aliases.
ServiceCollectionExtensions.AddKeyedAlias<TService,TSource>(this IServiceCollection, object, object) Method
Creates a keyed alias registration that allows resolving a service through a different interface or type with a specific key.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddKeyedAlias<TService,TSource>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, object? serviceKey, object? sourceKey=null)
where TService : class
where TSource : class;Type parameters
TService
The service type for the alias registration.
TSource
The source service type that is already registered.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
serviceKey System.Object
The service key to associate with the alias.
sourceKey System.Object
The service key of the source registration. Use null for non-keyed source.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
Exceptions
System.InvalidOperationException
Thrown when no registration is found for TSource with the specified sourceKey.
Remarks
This method creates a keyed alias by cloning the source service descriptor with a new service type and key. The alias preserves the lifetime of the source registration (Singleton, Scoped, or Transient).
For factory-based registrations, the alias resolves through the source service to maintain proper lifetime semantics.
ServiceCollectionExtensions.AddScoped<T>(this IServiceCollection, Dependency[]) Method
Registers a scoped service of the type specified in T with custom dependencies. A scoped service is created once per request within the scope.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddScoped<T>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, params Abblix.DependencyInjection.Dependency[] dependencies)
where T : class;Type parameters
T
The type of the service to add.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
dependencies Dependency[]
An array of Dependency objects representing additional dependencies required by the service.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
ServiceCollectionExtensions.AddScoped<TService,TImplementation>(this IServiceCollection, Dependency[]) Method
Registers a scoped service with the implementation type specified in TImplementation and the service type specified in TService with custom dependencies. A scoped service is created once per request within the scope.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddScoped<TService,TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, params Abblix.DependencyInjection.Dependency[] dependencies)
where TService : class
where TImplementation : class, TService;Type parameters
TService
The type of the service to add.
TImplementation
The type of the implementation to use.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
dependencies Dependency[]
An array of Dependency objects representing additional dependencies required by the service.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
ServiceCollectionExtensions.AddSingleton<T>(this IServiceCollection, Dependency[]) Method
Registers a singleton service of the type specified in T with custom dependencies. A singleton service is created the first time it is requested, and subsequent requests use the same instance.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddSingleton<T>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, params Abblix.DependencyInjection.Dependency[] dependencies)
where T : class;Type parameters
T
The type of the service to add.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
dependencies Dependency[]
An array of Dependency objects representing additional dependencies required by the service.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
ServiceCollectionExtensions.AddSingleton<TService,TImplementation>(this IServiceCollection, Dependency[]) Method
Registers a singleton service with the implementation type specified in TImplementation and the service type specified in TService with custom dependencies.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddSingleton<TService,TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, params Abblix.DependencyInjection.Dependency[] dependencies)
where TService : class
where TImplementation : class, TService;Type parameters
TService
The type of the service to add.
TImplementation
The type of the implementation to use.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
dependencies Dependency[]
An array of Dependency objects representing additional dependencies required by the service.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
ServiceCollectionExtensions.AddTransient<T>(this IServiceCollection, Dependency[]) Method
Registers a transient service of the type specified in T with custom dependencies. Transient services are created each time they are requested.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddTransient<T>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, params Abblix.DependencyInjection.Dependency[] dependencies)
where T : class;Type parameters
T
The service type to register.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
dependencies Dependency[]
The dependencies required by the service.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
Remarks
This overload is useful when the service type and implementation type are the same.
ServiceCollectionExtensions.AddTransient<TService,TImplementation>(this IServiceCollection, Dependency[]) Method
Registers a transient service with the implementation type specified in TImplementation and the service type specified in TService with custom dependencies. Transient services are created each time they are requested.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddTransient<TService,TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, params Abblix.DependencyInjection.Dependency[] dependencies)
where TService : class
where TImplementation : class, TService;Type parameters
TService
The type of the service to add.
TImplementation
The type of the implementation to use.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
dependencies Dependency[]
The dependencies required by the service.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
ServiceCollectionExtensions.ChangeLifetime<T>(this IServiceCollection, ServiceLifetime) Method
Changes the lifetime of a registered service of type T to the specified lifetime.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection ChangeLifetime<T>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime);Type parameters
T
The service type whose lifetime is to be changed.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection to operate on.
lifetime Microsoft.Extensions.DependencyInjection.ServiceLifetime
The new Microsoft.Extensions.DependencyInjection.ServiceLifetime to apply.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The modified Microsoft.Extensions.DependencyInjection.IServiceCollection instance.
Exceptions
System.InvalidOperationException
Thrown if the service descriptor for T has an unexpected format.
Remarks
Useful when you need to override the lifetime of an existing registration (e.g., from Singleton to Scoped).
ServiceCollectionExtensions.Clone(this ServiceDescriptor, Type) Method
Creates a copy of the service descriptor with a different service type while preserving the implementation and lifetime. For factory-based registrations, resolves through the source service type to maintain same-instance semantics across all aliases.
public static Microsoft.Extensions.DependencyInjection.ServiceDescriptor Clone(this Microsoft.Extensions.DependencyInjection.ServiceDescriptor source, System.Type serviceType);Parameters
source Microsoft.Extensions.DependencyInjection.ServiceDescriptor
The source service descriptor to clone.
serviceType System.Type
The service type for the cloned descriptor.
Returns
Microsoft.Extensions.DependencyInjection.ServiceDescriptor
A new service descriptor with the specified service type.
Exceptions
System.InvalidOperationException
Thrown when the source descriptor has an invalid configuration.
ServiceCollectionExtensions.CloneKeyed(this ServiceDescriptor, Type, object) Method
Creates a copy of the keyed service descriptor with a different service type and key while preserving the implementation and lifetime. For factory-based registrations, resolves through the source service type and key to maintain same-instance semantics across all aliases.
public static Microsoft.Extensions.DependencyInjection.ServiceDescriptor CloneKeyed(this Microsoft.Extensions.DependencyInjection.ServiceDescriptor source, System.Type serviceType, object? serviceKey);Parameters
source Microsoft.Extensions.DependencyInjection.ServiceDescriptor
The source service descriptor to clone.
serviceType System.Type
The service type for the cloned descriptor.
serviceKey System.Object
The service key for the cloned descriptor.
Returns
Microsoft.Extensions.DependencyInjection.ServiceDescriptor
A new keyed service descriptor with the specified service type and key.
Exceptions
System.InvalidOperationException
Thrown when the source descriptor has an invalid configuration.
ServiceCollectionExtensions.Compose<TInterface,TComposite>(this IServiceCollection, Dependency[]) Method
Composes a service type with multiple implementations into a single composite service.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection Compose<TInterface,TComposite>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, params Abblix.DependencyInjection.Dependency[] dependencies)
where TInterface : class
where TComposite : class, TInterface;Type parameters
TInterface
The interface type to be composed.
TComposite
The composite implementation type.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
dependencies Dependency[]
The dependencies required by the composite service.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
Remarks
This method replaces the registrations of a service type with a single composite registration over them. The composite type must have a constructor that accepts an array of the interface type. The existing registrations move to keyed ones that the composite resolves in registration order; being keyed also hides them from plain resolution, so the singular resolve yields only the composite. Their key is private to this library and deliberately unnamed here: a key written down in documentation goes stale, and a stale key answers a lookup with an empty sequence rather than an error. The family thus remains descriptor data in the collection rather than a snapshot captured in a closure: Decompose<TInterface>(this IServiceCollection) returns a live cursor over that data, and edits through it reach the composite at resolve - without the host ever naming the composite type. Members keep their own lifetimes and the composite adopts the shortest among them, so a longer-lived member is simply shared; only a member shorter-lived than the composite is rejected, since a composite may not capture something that dies before it.
ServiceCollectionExtensions.ComposeKeyed<TInterface,TComposite>(this IServiceCollection, object, Dependency[]) Method
Composes keyed implementations of TInterface registered under serviceKey into a single composite resolvable under that same key - the keyed counterpart of Compose<TInterface,TComposite>(this IServiceCollection, Dependency[]). The members move to keyed registrations under the family's own key, so same-interface families under different keys stay isolated and the family remains editable descriptor data for DecomposeKeyed<TInterface>(this IServiceCollection, object).
public static Microsoft.Extensions.DependencyInjection.IServiceCollection ComposeKeyed<TInterface,TComposite>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, object serviceKey, params Abblix.DependencyInjection.Dependency[] dependencies)
where TInterface : class
where TComposite : class, TInterface;Type parameters
TInterface
The interface type to be composed.
TComposite
The composite implementation type.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
serviceKey System.Object
The service key whose registrations form the family.
dependencies Dependency[]
The dependencies required by the composite service.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
Exceptions
System.InvalidOperationException
The family is already composed under this key.
ServiceCollectionExtensions.CreateService(this IServiceProvider, Type, Dependency[]) Method
Creates an instance of the specified type using the provided dependencies.
public static object CreateService(this System.IServiceProvider serviceProvider, System.Type type, params Abblix.DependencyInjection.Dependency[] dependencies);Parameters
serviceProvider System.IServiceProvider
The service provider to resolve required services from.
type System.Type
The type of service to create.
dependencies Dependency[]
A list of explicitly provided dependencies.
Returns
System.Object
An instance of the specified type.
Remarks
This method allows partial control over dependency injection by mixing resolved and custom parameters. Useful in plugin scenarios, factory setups, or advanced test setups.
ServiceCollectionExtensions.CreateService<T>(this IServiceProvider, Dependency[]) Method
Creates an instance of the specified service type T using the provided dependencies.
public static T CreateService<T>(this System.IServiceProvider serviceProvider, params Abblix.DependencyInjection.Dependency[] dependencies);Type parameters
T
The type of service to create.
Parameters
serviceProvider System.IServiceProvider
The service provider to resolve required services from.
dependencies Dependency[]
A list of explicitly provided dependencies.
Returns
Remarks
This overload simplifies strongly typed creation of services with custom dependency injection.
ServiceCollectionExtensions.Decompose<TInterface>(this IServiceCollection) Method
Opens the TInterface family for in-place editing: returns a live
IComposition<TInterface> cursor over its members. Insert, remove or reorder through the
cursor and the change is live - a composed family's composite reads its members via
GetKeyedServices at resolve, so the edit takes effect with no separate recompose.
public static Abblix.DependencyInjection.IComposition<TInterface> Decompose<TInterface>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services)
where TInterface : class;Type parameters
TInterface
The family interface type.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection holding the family.
Returns
Abblix.DependencyInjection.IComposition<TInterface>
A live cursor over the family's members, in execution order.
Exceptions
System.InvalidOperationException
The family is composed but what a composition leaves behind
is no longer intact - either the mark over its members or the composite that heads them has been removed
from the collection. No sequence of calls on this API produces that.
Remarks
Whether the family has been composed is the cursor's business, not the caller's: editing its members is
the only reason to ask for one. Composed, the members live as keyed descriptors under a key private to
this library, which hides them from plain resolution; this cursor and its resolve-time counterpart
Decompose<TInterface>(this IServiceProvider, object) are the only ways to reach them. Uncomposed,
they are the plain descriptors of the interface, and a later Compose takes them as they stand. The cursor edits whichever of the two the family currently holds, so the same call adds a
member before and after composition.
IComposition<TInterface> adds position-aware sugar (AddAfter, AddBefore,
Remove, ...); anchors are matched by implementation type via
ResolveImplementationType(this ServiceDescriptor), which identifies a member even when it was registered through a
typed factory (e.g. by TryAddEnumerableAlias<TService,TImplementation>(this IServiceCollection)).
services.Decompose<IPipelineStep>()
.AddAfter<BuiltInStep>(ServiceDescriptor.Singleton<IPipelineStep, MyStep>())
.Remove<UnwantedStep>();ServiceCollectionExtensions.Decompose<TInterface>(this IServiceProvider, object) Method
The members of the TInterface family as the container will run them - the resolve-time counterpart of Decompose<TInterface>(this IServiceCollection), which edits the same family before the container is built.
public static System.Collections.Generic.IReadOnlyList<TInterface> Decompose<TInterface>(this System.IServiceProvider serviceProvider, object? serviceKey=null)
where TInterface : class;Type parameters
TInterface
The family interface.
Parameters
serviceProvider System.IServiceProvider
The provider built from the collection the family lives in.
serviceKey System.Object
The key a keyed family lives under, or null for a plain family.
Returns
System.Collections.Generic.IReadOnlyList<TInterface>
The family's members, in execution order.
Exceptions
System.InvalidOperationException
The family has no members.
Remarks
Whether the family was composed is this method's business, not the caller's: composed, the members are keyed and hidden from plain resolution; loose, they are the plain registrations. Asking for them by the key composition happens to use is what a caller must not do, because a key that is merely out of date answers with an empty set rather than an error - and an empty set reads as a family with nothing in it.
For that reason an empty family is refused rather than returned. A caller that wants a possibly-empty answer is asking about registrations rather than about a family, and Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetServices<>.IServiceProvider) is that question.
ServiceCollectionExtensions.DecomposeKeyed<TInterface>(this IServiceCollection, object) Method
Opens the keyed TInterface family for in-place editing: returns a live
IComposition<TInterface> cursor over its members. Insert, remove or reorder through the
cursor and the change is live - a composed family's keyed composite reads its members via
GetKeyedServices at resolve, so the edit takes effect with no separate recompose. Uncomposed, the
members are the descriptors registered under the service key itself, and the cursor edits those.
public static Abblix.DependencyInjection.IComposition<TInterface> DecomposeKeyed<TInterface>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, object serviceKey)
where TInterface : class;Type parameters
TInterface
The composed interface type.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection holding the composed family.
serviceKey System.Object
The service key the family was composed under.
Returns
Abblix.DependencyInjection.IComposition<TInterface>
A live cursor over the family's members, in execution order.
Exceptions
System.InvalidOperationException
The family is composed under this key but what a composition
leaves behind is no longer intact - either the mark over its members or the composite that heads them has
been removed from the collection. No sequence of calls on this API produces that.
Remarks
The mechanics mirror Decompose<TInterface>(this IServiceCollection), except the family's key carries the service
key too, so pipelines of the same interface under different keys stay isolated.
IComposition<TInterface> adds the same position-aware sugar
(AddAfter, Remove, ...).
ServiceCollectionExtensions.Decorate<TInterface,TDecorator>(this IServiceCollection, Dependency[]) Method
Decorates a registered service with a decorator implementation.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection Decorate<TInterface,TDecorator>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, params Abblix.DependencyInjection.Dependency[] dependencies)
where TInterface : class
where TDecorator : class, TInterface;Type parameters
TInterface
The service type to be decorated.
TDecorator
The decorator implementation type.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
dependencies Dependency[]
The dependencies required by the decorator.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
Remarks
The decorator pattern allows you to add behavior to existing service implementations without modifying their code. The decorator wraps the original service and preserves its lifetime registration. The decorator must implement the same interface as the service being decorated.
ServiceCollectionExtensions.DecorateKeyed<TInterface,TDecorator>(this IServiceCollection, object, Dependency[]) Method
Decorates a registered keyed service with a decorator implementation. If no keyed service is found, falls back to decorating the non-keyed service and registers the result as a keyed service.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection DecorateKeyed<TInterface,TDecorator>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, object? serviceKey, params Abblix.DependencyInjection.Dependency[] dependencies)
where TInterface : class
where TDecorator : class, TInterface;Type parameters
TInterface
The service type to be decorated.
TDecorator
The decorator implementation type.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
serviceKey System.Object
The service key for the decorated service registration.
dependencies Dependency[]
The dependencies required by the decorator.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
Remarks
This method allows decoration of keyed services registered using the keyed service APIs. If the keyed service is not found, it falls back to decorating the non-keyed service and registers the decorator as a keyed service. This is useful when you want to create a keyed variant of an existing non-keyed service with additional behavior. The decorator will wrap the existing implementation while preserving the service lifetime.
ServiceCollectionExtensions.Find<T>(this IServiceCollection) Method
Finds the registered service descriptor for type T, or returns null if
none exists.
public static Microsoft.Extensions.DependencyInjection.ServiceDescriptor? Find<T>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);Type parameters
T
The service type to locate.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection to search.
Returns
Microsoft.Extensions.DependencyInjection.ServiceDescriptor
The matching Microsoft.Extensions.DependencyInjection.ServiceDescriptor, or null if not found.
ServiceCollectionExtensions.FindAll<T>(this IServiceCollection) Method
Finds all registered service descriptors for type T.
public static System.Collections.Generic.IEnumerable<Microsoft.Extensions.DependencyInjection.ServiceDescriptor> FindAll<T>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);Type parameters
T
The service type to locate.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection to search.
Returns
System.Collections.Generic.IEnumerable<Microsoft.Extensions.DependencyInjection.ServiceDescriptor>
An System.Collections.Generic.IEnumerable<> of matching Microsoft.Extensions.DependencyInjection.ServiceDescriptor instances.
ServiceCollectionExtensions.FindRequired<T>(this IServiceCollection) Method
Finds the first registered service descriptor for type T. Throws if no descriptor is found.
public static Microsoft.Extensions.DependencyInjection.ServiceDescriptor FindRequired<T>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);Type parameters
T
The service type to locate.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection to search.
Returns
Microsoft.Extensions.DependencyInjection.ServiceDescriptor
The matching Microsoft.Extensions.DependencyInjection.ServiceDescriptor.
Exceptions
System.InvalidOperationException
Thrown if no descriptor is found for the specified type.
ServiceCollectionExtensions.RemoveAll<T>(this IServiceCollection) Method
Removes all registrations of type T from the service collection.
public static Microsoft.Extensions.DependencyInjection.ServiceDescriptor[] RemoveAll<T>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);Type parameters
T
The service type to remove.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection to operate on.
Returns
Microsoft.Extensions.DependencyInjection.ServiceDescriptor[]
true if any descriptors were removed; otherwise, false.
Remarks
Can be used to clean up pre-registered services before adding custom implementations.
ServiceCollectionExtensions.ResolveImplementationType(this ServiceDescriptor) Method
Stand-in for the internal ServiceDescriptor.GetImplementationType(): returns the
implementation type whether the descriptor was registered with an explicit
implementation type, an implementation instance, or a typed factory
Func<IServiceProvider, TImpl> (.NET 10 generic AddSingleton uses the last shape,
so the property alone returns null for those registrations). Supports both plain and keyed
descriptors - for keyed ones the type is derived from the Keyed* counterparts, including
the keyed factory shape Func<IServiceProvider, object?, TImpl> produced when
Compose<TInterface,TComposite>(this IServiceCollection, Dependency[]) moves a family
member into its keyed registration.
public static System.Type? ResolveImplementationType(this Microsoft.Extensions.DependencyInjection.ServiceDescriptor descriptor);Parameters
descriptor Microsoft.Extensions.DependencyInjection.ServiceDescriptor
The descriptor whose implementation type to derive.
Returns
System.Type
The implementation type, or null when it cannot be derived (untyped factory).
ServiceCollectionExtensions.TryAddAlias<TService,TImplementation>(this IServiceCollection) Method
Adds TService as a SHARED-instance alias for the existing
TImplementation registration - unless TService
is already registered. Sister of AddAlias<TService,TImplementation>(this IServiceCollection) with
TryAdd semantics on the alias service type: a host pre-registration of the aliased
contract wins, which keeps the library-wide "host pre-registration wins" convention on
singular seams whose library default is routed through an alias. Use plain
AddAlias<TService,TImplementation>(this IServiceCollection) only where the alias must be added
unconditionally (e.g. composition machinery that appends to an enumerable set).
public static Microsoft.Extensions.DependencyInjection.IServiceCollection TryAddAlias<TService,TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services)
where TService : class
where TImplementation : class, TService;Type parameters
TService
The service type to register the alias under.
TImplementation
The implementation type already registered as a concrete (or as another service) in the service collection.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add to.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection so additional calls can be chained.
Exceptions
System.InvalidOperationException
Thrown when no registration is found for TImplementation.
ServiceCollectionExtensions.TryAddEnumerableAlias<TService,TImplementation>(this IServiceCollection) Method
Adds TService to an enumerable strategy set as a SHARED-instance
alias for the existing TImplementation registration. Sister of
AddAlias<TService,TImplementation>(this IServiceCollection): same semantic of "route this service
to that already-registered impl", but adds via TryAddEnumerable (so repeated
calls dedupe on (ServiceType, ImplementationType)) and always uses a typed
factory delegate that resolves through the source registration - guaranteeing the
alias and the source share one instance.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection TryAddEnumerableAlias<TService,TImplementation>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services)
where TService : class
where TImplementation : class, TService;Type parameters
TService
The enumerable service type to register the alias under.
TImplementation
The implementation type already registered as a concrete (or as another TService) in the service collection.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add to.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection so additional calls can be chained.
Exceptions
System.InvalidOperationException
No registration was found for
TImplementation.
ServiceCollectionExtensions.TryAddKeyedAlias<TService,TSource>(this IServiceCollection, object, object) Method
Creates a keyed alias registration for TService under
serviceKey - unless that (service type, key) pair is already
registered. Sister of AddKeyedAlias<TService,TSource>(this IServiceCollection, object, object) with TryAdd
semantics on the alias identity: a pre-existing registration under the same key wins,
mirroring TryAddAlias<TService,TImplementation>(this IServiceCollection) for keyed seams and
keeping the library-wide "host pre-registration wins" convention.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection TryAddKeyedAlias<TService,TSource>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, object? serviceKey, object? sourceKey=null)
where TService : class
where TSource : class;Type parameters
TService
The service type for the alias registration.
TSource
The source service type that is already registered.
Parameters
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
serviceKey System.Object
The service key to associate with the alias.
sourceKey System.Object
The service key of the source registration. Use null for non-keyed source.
Returns
Microsoft.Extensions.DependencyInjection.IServiceCollection
The updated Microsoft.Extensions.DependencyInjection.IServiceCollection.
Exceptions
System.InvalidOperationException
Thrown when no registration is found for TSource with the specified sourceKey.