Skip to content

ServiceProviderExtensions

NexusLabs.Needlr

NexusLabs.Needlr

ServiceProviderExtensions Class

Extension methods for System.IServiceProvider to provide type inspection functionality.

public static class ServiceProviderExtensions

Inheritance System.Object 🡒 ServiceProviderExtensions

Methods

ServiceProviderExtensions.CopyRegistrationsToServiceCollection(this IServiceProvider) Method

Copies all service registrations from the service provider to a new service collection.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection CopyRegistrationsToServiceCollection(this System.IServiceProvider serviceProvider);

Parameters

serviceProvider System.IServiceProvider

The source service provider.

Returns

Microsoft.Extensions.DependencyInjection.IServiceCollection
A new service collection containing all registrations.

ServiceProviderExtensions.CopyRegistrationsToServiceCollection(this IServiceProvider, IServiceCollection) Method

Copies all service registrations from the service provider to the specified service collection.

public static void CopyRegistrationsToServiceCollection(this System.IServiceProvider serviceProvider, Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection);

Parameters

serviceProvider System.IServiceProvider

The source service provider.

serviceCollection Microsoft.Extensions.DependencyInjection.IServiceCollection

The target service collection.

ServiceProviderExtensions.GetRegisteredTypes(this IServiceProvider, Func<Type,bool>) Method

Gets all registered service types that match the specified predicate without instantiating the services.

public static System.Collections.Generic.IEnumerable<System.Type> GetRegisteredTypes(this System.IServiceProvider serviceProvider, System.Func<System.Type,bool> predicate);

Parameters

serviceProvider System.IServiceProvider

The service provider to inspect.

predicate System.Func<System.Type,System.Boolean>

A function to filter the service types.

Returns

System.Collections.Generic.IEnumerable<System.Type>
A read-only list of types that match the predicate.

Exceptions

System.ArgumentNullException
Thrown when serviceProvider or predicate is null.

System.InvalidOperationException
Thrown when the service collection is not accessible.

Example

// Get all interface types
var interfaceTypes = serviceProvider.GetRegisteredTypes(type => type.IsInterface);

// Get all types in a specific namespace
var myNamespaceTypes = serviceProvider.GetRegisteredTypes(type => 
    type.Namespace?.StartsWith("MyApp.Services") == true);

// Get all types implementing a specific interface
var repositoryTypes = serviceProvider.GetRegisteredTypes(type => 
    typeof(IRepository).IsAssignableFrom(type));

ServiceProviderExtensions.GetRegisteredTypesOf<T>(this IServiceProvider) Method

Gets all registered service types that implement or inherit from the specified type without instantiating the services.

public static System.Collections.Generic.IEnumerable<System.Type> GetRegisteredTypesOf<T>(this System.IServiceProvider serviceProvider);

Type parameters

T

The base type or interface to filter by.

Parameters

serviceProvider System.IServiceProvider

The service provider to inspect.

Returns

System.Collections.Generic.IEnumerable<System.Type>
A read-only list of types that implement or inherit from T.

Exceptions

System.ArgumentNullException
Thrown when serviceProvider is null.

Example

// Get all types implementing IRepository
var repositoryTypes = serviceProvider.GetRegisteredTypesOf<IRepository>();

// Get all types inheriting from BaseService
var serviceTypes = serviceProvider.GetRegisteredTypesOf<BaseService>();

ServiceProviderExtensions.GetServiceCollection(this IServiceProvider) Method

Gets the service collection from the service provider.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection GetServiceCollection(this System.IServiceProvider serviceProvider);

Parameters

serviceProvider System.IServiceProvider

The service provider.

Returns

Microsoft.Extensions.DependencyInjection.IServiceCollection
The service collection.

Exceptions

System.InvalidOperationException
Thrown when the service collection is not available.

ServiceProviderExtensions.GetServiceRegistrations(this IServiceProvider) Method

Gets detailed information about all registered services.

public static System.Collections.Generic.IReadOnlyList<NexusLabs.Needlr.ServiceRegistrationInfo> GetServiceRegistrations(this System.IServiceProvider serviceProvider);

Parameters

serviceProvider System.IServiceProvider

The service provider to inspect.

Returns

System.Collections.Generic.IReadOnlyList<ServiceRegistrationInfo>
A read-only list of service registration information.

Exceptions

System.ArgumentNullException
Thrown when serviceProvider is null.

Example

// Get all singleton services
var singletons = serviceProvider.GetServiceRegistrations(
    descriptor => descriptor.Lifetime == ServiceLifetime.Singleton);

// Get all services with a specific implementation type
var specificImpls = serviceProvider.GetServiceRegistrations(
    descriptor => descriptor.ImplementationType == typeof(MyService));

ServiceProviderExtensions.GetServiceRegistrations(this IServiceProvider, 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 System.IServiceProvider serviceProvider, System.Func<Microsoft.Extensions.DependencyInjection.ServiceDescriptor,bool> predicate);

Parameters

serviceProvider System.IServiceProvider

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 serviceProvider or predicate is null.

Example

// Get all singleton services
var singletons = serviceProvider.GetServiceRegistrations(
    descriptor => descriptor.Lifetime == ServiceLifetime.Singleton);

// Get all services with a specific implementation type
var specificImpls = serviceProvider.GetServiceRegistrations(
    descriptor => descriptor.ImplementationType == typeof(MyService));

ServiceProviderExtensions.IsRegistered(this IServiceProvider, Type) Method

Determines whether a service of the specified type is registered in the service provider.

public static bool IsRegistered(this System.IServiceProvider serviceProvider, System.Type serviceType);

Parameters

serviceProvider System.IServiceProvider

The service provider to check.

serviceType System.Type

The service type to check.

Returns

System.Boolean
True if the service is registered; otherwise, false.

ServiceProviderExtensions.IsRegistered<TService>(this IServiceProvider) Method

Determines whether a service of the specified type is registered in the service provider.

public static bool IsRegistered<TService>(this System.IServiceProvider serviceProvider);

Type parameters

TService

The service type to check.

Parameters

serviceProvider System.IServiceProvider

The service provider to check.

Returns

System.Boolean
True if the service is registered; otherwise, false.