ServiceCollectionExtensions
NexusLabs.Needlr¶
NexusLabs.Needlr¶
ServiceCollectionExtensions Class¶
Extension methods for Microsoft.Extensions.DependencyInjection.IServiceCollection that add decorator wiring, service inspection, and registration-check utilities to the standard Microsoft DI container.
Inheritance System.Object 🡒 ServiceCollectionExtensions
Remarks¶
The primary extension in this class is AddDecorator, which wraps an already-registered
service with a decorator while preserving the original service's lifetime. This complements
the attribute-based DecoratorForAttribute<TService> used with Needlr's
source generation and reflection scanning.
Methods¶
ServiceCollectionExtensions.AddDecorator(this IServiceCollection, Type, Type) Method¶
Decorates an existing service registration with a decorator type, preserving the original service's lifetime. The decorator must implement the service interface and take the service interface as a constructor parameter. Works with both interfaces and class types.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDecorator(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Type serviceType, System.Type decoratorType);
Parameters¶
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection to modify.
serviceType System.Type
The service type (interface or class) to decorate.
decoratorType System.Type
The decorator type that implements the service type.
Returns¶
Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection for method chaining.
Exceptions¶
System.ArgumentNullException
Thrown when services, serviceType, or decoratorType is null.
System.InvalidOperationException
Thrown when no service registration is found for the service type.
Example¶
// Register the original service
services.AddScoped<IMyService, MyService>();
// Decorate it while preserving the scoped lifetime
services.AddDecorator(typeof(IMyService), typeof(MyServiceDecorator));
ServiceCollectionExtensions.AddDecorator<TService,TDecorator>(this IServiceCollection) Method¶
Decorates an existing service registration with a decorator type, preserving the original service's lifetime. The decorator must implement the service interface and take the service interface as a constructor parameter. Works with both interfaces and class types.
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDecorator<TService,TDecorator>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services)
where TDecorator : class, TService;
Type parameters¶
TService
The service type (interface or class) to decorate.
TDecorator
The decorator type that implements TService.
Parameters¶
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection to modify.
Returns¶
Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection for method chaining.
Exceptions¶
System.ArgumentNullException
Thrown when services is null.
System.InvalidOperationException
Thrown when no service registration is found for TService.
Example¶
// Register the original service
services.AddScoped<IMyService, MyService>();
// Decorate it while preserving the scoped lifetime
services.AddDecorator<IMyService, MyServiceDecorator>();
ServiceCollectionExtensions.GetServiceRegistrations(this IServiceCollection) Method¶
Gets detailed information about all registered services.
public static System.Collections.Generic.IReadOnlyList<NexusLabs.Needlr.ServiceRegistrationInfo> GetServiceRegistrations(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection);
Parameters¶
serviceCollection Microsoft.Extensions.DependencyInjection.IServiceCollection
The service provider to inspect.
Returns¶
System.Collections.Generic.IReadOnlyList<ServiceRegistrationInfo>
A read-only list of service registration information.
Exceptions¶
System.ArgumentNullException
Thrown when serviceCollection is null.
Example¶
// Get all singleton services
var singletons = serviceCollection.GetServiceRegistrations(
descriptor => descriptor.Lifetime == ServiceLifetime.Singleton);
// Get all services with a specific implementation type
var specificImpls = serviceCollection.GetServiceRegistrations(
descriptor => descriptor.ImplementationType == typeof(MyService));
ServiceCollectionExtensions.GetServiceRegistrations(this IServiceCollection, Func<ServiceDescriptor,bool>) Method¶
Gets detailed information about all registered services that match the specified predicate.
public static System.Collections.Generic.IReadOnlyList<NexusLabs.Needlr.ServiceRegistrationInfo> GetServiceRegistrations(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, System.Func<Microsoft.Extensions.DependencyInjection.ServiceDescriptor,bool> predicate);
Parameters¶
serviceCollection Microsoft.Extensions.DependencyInjection.IServiceCollection
The service provider to inspect.
predicate System.Func<Microsoft.Extensions.DependencyInjection.ServiceDescriptor,System.Boolean>
A function to filter the service descriptors.
Returns¶
System.Collections.Generic.IReadOnlyList<ServiceRegistrationInfo>
A read-only list of service registration information.
Exceptions¶
System.ArgumentNullException
Thrown when serviceCollection or predicate is null.
Example¶
// Get all singleton services
var singletons = serviceCollection.GetServiceRegistrations(
descriptor => descriptor.Lifetime == ServiceLifetime.Singleton);
// Get all services with a specific implementation type
var specificImpls = serviceCollection.GetServiceRegistrations(
descriptor => descriptor.ImplementationType == typeof(MyService));
ServiceCollectionExtensions.IsRegistered(this IServiceCollection, Type) Method¶
Determines whether a service of the specified type is registered in the service collection.
public static bool IsRegistered(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Type serviceType);
Parameters¶
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection to check.
serviceType System.Type
The service type to check.
Returns¶
System.Boolean
True if the service is registered; otherwise, false.
ServiceCollectionExtensions.IsRegistered<TService>(this IServiceCollection) Method¶
Determines whether a service of the specified type is registered in the service collection.
public static bool IsRegistered<TService>(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);
Type parameters¶
TService
The service type to check.
Parameters¶
services Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection to check.
Returns¶
System.Boolean
True if the service is registered; otherwise, false.