Class ServiceCollectionExtensions
- Namespace
- Invex.Extensions.Hosting
- Assembly
- Invex.Extensions.Hosting.dll
Provides extension methods for Microsoft.Extensions.DependencyInjection.IServiceCollection that register a single implementation type under multiple service types ("forwarded" registrations) while sharing one instance per lifetime.
[PublicAPI]
public static class ServiceCollectionExtensions
- Inheritance
-
objectServiceCollectionExtensions
Examples
// MyService implements both IFoo and IBar; both resolve to the same singleton.
services.AddSingleton<IFoo, IBar, MyService>();
// All three resolve to the same object:
var foo = provider.GetRequiredService<IFoo>();
var bar = provider.GetRequiredService<IBar>();
var service = provider.GetRequiredService<MyService>();
// MyWorker implements IWorkerStatus and IHostedService; the running hosted service
// instance is also resolvable as IWorkerStatus.
services.AddHostedService<IWorkerStatus, MyWorker>();
Remarks
The standard AddSingleton/AddScoped methods create an independent instance per service type
when the same implementation is registered multiple times. The overloads in this class instead register the
implementation type once and forward each service type to it, so resolving any of the service types yields
the same instance (per scope, for scoped registrations). TImplementation itself is
also resolvable and returns that same instance. Singleton registrations share one instance for the
application lifetime; scoped registrations share one instance within each scope, and different scopes
receive different instances.
The AddHostedService overloads additionally register the implementation as an
Microsoft.Extensions.Hosting.IHostedService, allowing other components to interact with the running hosted service
through its service interfaces.
The container may dispose the shared instance more than once: it tracks every disposable object returned
by a factory registration, and each forwarded service type is a factory registration. When the provider
(or scope) is disposed, Dispose/DisposeAsync is called once for the TImplementation
registration and once for each forwarded registration that was resolved, so implementations should make
disposal idempotent.
Registrations are appended unconditionally, like the framework's own AddSingleton and
AddScoped methods; they are not conditional TryAdd registrations.
In the net10.0 build, the TImplementation type parameter of every overload is
annotated with [DynamicallyAccessedMembers]
(DynamicallyAccessedMemberTypes.PublicConstructors), so the constructors required by the
dependency injection container are preserved under trimming.
Methods
AddHostedService<TService, TImplementation>(IServiceCollection)
Adds a hosted service of the types specified in TService, with an
implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddHostedService<TService, TImplementation>(this IServiceCollection serviceCollection) where TService : class where TImplementation : class, TService, IHostedService
Parameters
serviceCollectionIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TServiceThe type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
TImplementation is registered as a singleton and as a hosted service.
Every service type and the hosted service resolve to the same shared instance, allowing other components
to interact with the running hosted service through its service interfaces.
- See Also
-
Singleton
AddHostedService<TService1, TService2, TImplementation>(IServiceCollection)
Adds a hosted service of the types specified in TService1,
TService2, with an
implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddHostedService<TService1, TService2, TImplementation>(this IServiceCollection serviceCollection) where TService1 : class where TService2 : class where TImplementation : class, TService1, TService2, IHostedService
Parameters
serviceCollectionIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
TImplementation is registered as a singleton and as a hosted service.
Every service type and the hosted service resolve to the same shared instance, allowing other components
to interact with the running hosted service through its service interfaces.
- See Also
-
Singleton
AddHostedService<TService1, TService2, TService3, TImplementation>(IServiceCollection)
Adds a hosted service of the types specified in TService1,
TService2,
TService3,
with an implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddHostedService<TService1, TService2, TService3, TImplementation>(this IServiceCollection serviceCollection) where TService1 : class where TService2 : class where TService3 : class where TImplementation : class, TService1, TService2, TService3, IHostedService
Parameters
serviceCollectionIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TService3The third type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
TImplementation is registered as a singleton and as a hosted service.
Every service type and the hosted service resolve to the same shared instance, allowing other components
to interact with the running hosted service through its service interfaces.
- See Also
-
Singleton
AddHostedService<TService1, TService2, TService3, TService4, TImplementation>(IServiceCollection)
Adds a hosted service of the types specified in TService1,
TService2,
TService3, TService4,
with an implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddHostedService<TService1, TService2, TService3, TService4, TImplementation>(this IServiceCollection serviceCollection) where TService1 : class where TService2 : class where TService3 : class where TService4 : class where TImplementation : class, TService1, TService2, TService3, TService4, IHostedService
Parameters
serviceCollectionIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TService3The third type of the service to add.
TService4The fourth type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
TImplementation is registered as a singleton and as a hosted service.
Every service type and the hosted service resolve to the same shared instance, allowing other components
to interact with the running hosted service through its service interfaces.
- See Also
-
Singleton
AddHostedService<TService1, TService2, TService3, TService4, TService5, TImplementation>(IServiceCollection)
Adds a hosted service of the types specified in TService1,
TService2,
TService3, TService4, TService5,
with an implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddHostedService<TService1, TService2, TService3, TService4, TService5, TImplementation>(this IServiceCollection serviceCollection) where TService1 : class where TService2 : class where TService3 : class where TService4 : class where TService5 : class where TImplementation : class, TService1, TService2, TService3, TService4, TService5, IHostedService
Parameters
serviceCollectionIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TService3The third type of the service to add.
TService4The fourth type of the service to add.
TService5The fifth type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
TImplementation is registered as a singleton and as a hosted service.
Every service type and the hosted service resolve to the same shared instance, allowing other components
to interact with the running hosted service through its service interfaces.
- See Also
-
Singleton
AddScoped<TService1, TService2, TImplementation>(IServiceCollection)
Adds a scoped service of the types specified in TService1,
TService2
with an implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddScoped<TService1, TService2, TImplementation>(this IServiceCollection services) where TService1 : class where TService2 : class where TImplementation : class, TService1, TService2
Parameters
servicesIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
All service registrations will share the same instance of the implementation type within a scope.
- See Also
-
Scoped
AddScoped<TService1, TService2, TService3, TImplementation>(IServiceCollection)
Adds a scoped service of the types specified in TService1,
TService2,
TService3
with an implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddScoped<TService1, TService2, TService3, TImplementation>(this IServiceCollection services) where TService1 : class where TService2 : class where TService3 : class where TImplementation : class, TService1, TService2, TService3
Parameters
servicesIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TService3The third type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
All service registrations will share the same instance of the implementation type within a scope.
- See Also
-
Scoped
AddScoped<TService1, TService2, TService3, TService4, TImplementation>(IServiceCollection)
Adds a scoped service of the types specified in TService1,
TService2,
TService3, TService4
with an implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddScoped<TService1, TService2, TService3, TService4, TImplementation>(this IServiceCollection services) where TService1 : class where TService2 : class where TService3 : class where TService4 : class where TImplementation : class, TService1, TService2, TService3, TService4
Parameters
servicesIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TService3The third type of the service to add.
TService4The fourth type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
All service registrations will share the same instance of the implementation type within a scope.
- See Also
-
Scoped
AddScoped<TService1, TService2, TService3, TService4, TService5, TImplementation>(IServiceCollection)
Adds a scoped service of the types specified in TService1,
TService2,
TService3, TService4, TService5
with an implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddScoped<TService1, TService2, TService3, TService4, TService5, TImplementation>(this IServiceCollection services) where TService1 : class where TService2 : class where TService3 : class where TService4 : class where TService5 : class where TImplementation : class, TService1, TService2, TService3, TService4, TService5
Parameters
servicesIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TService3The third type of the service to add.
TService4The fourth type of the service to add.
TService5The fifth type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
All service registrations will share the same instance of the implementation type within a scope.
- See Also
-
Scoped
AddSingleton<TService1, TService2, TImplementation>(IServiceCollection)
Adds a singleton service of the types specified in TService1,
TService2
with an implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddSingleton<TService1, TService2, TImplementation>(this IServiceCollection services) where TService1 : class where TService2 : class where TImplementation : class, TService1, TService2
Parameters
servicesIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
All service registrations will share the same instance of the implementation type.
- See Also
-
Singleton
AddSingleton<TService1, TService2, TService3, TImplementation>(IServiceCollection)
Adds a singleton service of the types specified in TService1,
TService2,
TService3
with an implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddSingleton<TService1, TService2, TService3, TImplementation>(this IServiceCollection services) where TService1 : class where TService2 : class where TService3 : class where TImplementation : class, TService1, TService2, TService3
Parameters
servicesIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TService3The third type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
All service registrations will share the same instance of the implementation type.
- See Also
-
Singleton
AddSingleton<TService1, TService2, TService3, TService4, TImplementation>(IServiceCollection)
Adds a singleton service of the types specified in TService1,
TService2,
TService3, TService4
with an implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddSingleton<TService1, TService2, TService3, TService4, TImplementation>(this IServiceCollection services) where TService1 : class where TService2 : class where TService3 : class where TService4 : class where TImplementation : class, TService1, TService2, TService3, TService4
Parameters
servicesIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TService3The third type of the service to add.
TService4The fourth type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
All service registrations will share the same instance of the implementation type.
- See Also
-
Singleton
AddSingleton<TService1, TService2, TService3, TService4, TService5, TImplementation>(IServiceCollection)
Adds a singleton service of the types specified in TService1,
TService2,
TService3, TService4, TService5
with an implementation type specified in TImplementation to the specified
Microsoft.Extensions.DependencyInjection.IServiceCollection.
public static IServiceCollection AddSingleton<TService1, TService2, TService3, TService4, TService5, TImplementation>(this IServiceCollection services) where TService1 : class where TService2 : class where TService3 : class where TService4 : class where TService5 : class where TImplementation : class, TService1, TService2, TService3, TService4, TService5
Parameters
servicesIServiceCollectionThe Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.
Returns
- IServiceCollection
A reference to this instance after the operation has completed.
Type Parameters
TService1The first type of the service to add.
TService2The second type of the service to add.
TService3The third type of the service to add.
TService4The fourth type of the service to add.
TService5The fifth type of the service to add.
TImplementationThe type of the implementation to use.
Remarks
All service registrations will share the same instance of the implementation type.
- See Also
-
Singleton