Table of Contents

Class MicrosoftDependencyResolver

Namespace
Splat.Microsoft.Extensions.DependencyInjection
Assembly
Splat.Microsoft.Extensions.DependencyInjection.dll

Provides an implementation of the dependency resolver pattern using Microsoft.Extensions.DependencyInjection. Supports registration, resolution, and management of services with optional contract-based (keyed) registrations.

public class MicrosoftDependencyResolver : IDependencyResolver, IReadonlyDependencyResolver, IMutableDependencyResolver, IDisposable, IAsyncDisposable
Inheritance
MicrosoftDependencyResolver
Implements
Extension Methods

Remarks

This class enables integration with the Microsoft dependency injection container, allowing for both programmatic and externally provided service collections or providers. Once the container is built from an IServiceProvider, further modifications are not permitted. Thread safety is ensured for all registration and resolution operations. Contract-based (keyed) registrations are supported if the underlying service provider implements IKeyedServiceProvider. This resolver is suitable for scenarios requiring dynamic service registration and resolution, as well as integration with existing Microsoft.Extensions.DependencyInjection infrastructure.

Constructors

MicrosoftDependencyResolver(IServiceCollection?)

Initializes a new instance of the MicrosoftDependencyResolver class with an IServiceCollection.

public MicrosoftDependencyResolver(IServiceCollection? services = null)

Parameters

services IServiceCollection

An instance of IServiceCollection.

MicrosoftDependencyResolver(IServiceProvider)

Initializes a new instance of the MicrosoftDependencyResolver class with a configured service Provider.

public MicrosoftDependencyResolver(IServiceProvider serviceProvider)

Parameters

serviceProvider IServiceProvider

A ready to use service provider.

Properties

ServiceProvider

Gets the internal Microsoft container, or builds a new one if this instance was not initialized with one.

protected virtual IServiceProvider? ServiceProvider { get; }

Property Value

IServiceProvider

Methods

Dispose()

public void Dispose()

Dispose(bool)

Disposes of the instance.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

Whether or not the instance is disposing.

DisposeAsync()

public ValueTask DisposeAsync()

Returns

ValueTask

GetService(Type?)

Gets an instance of the given serviceType. Must return null if the service is not available (must not throw).

public virtual object? GetService(Type? serviceType)

Parameters

serviceType Type

The object type.

Returns

object

The requested object, if found; null otherwise.

GetService(Type?, string?)

Gets an instance of the given serviceType. Must return null if the service is not available (must not throw).

public virtual object? GetService(Type? serviceType, string? contract)

Parameters

serviceType Type

The object type.

contract string

A value which will retrieve only a object registered with the same contract.

Returns

object

The requested object, if found; null otherwise.

GetService<T>()

Gets an instance of the given T. Must return null if the service is not available (must not throw).

public T? GetService<T>()

Returns

T

The requested object, if found; null otherwise.

Type Parameters

T

The object type.

GetService<T>(string?)

Gets an instance of the given T. Must return null if the service is not available (must not throw).

public T? GetService<T>(string? contract)

Parameters

contract string

A value which will retrieve only a object registered with the same contract.

Returns

T

The requested object, if found; null otherwise.

Type Parameters

T

The object type.

GetServices(Type?)

Gets all instances of the given serviceType. Must return an empty collection if the service is not available (must not return null or throw).

public virtual IEnumerable<object> GetServices(Type? serviceType)

Parameters

serviceType Type

The object type.

Returns

IEnumerable<object>

A sequence of instances of the requested serviceType. The sequence should be empty (not null) if no objects of the given type are available.

GetServices(Type?, string?)

Gets all instances of the given serviceType. Must return an empty collection if the service is not available (must not return null or throw).

public virtual IEnumerable<object> GetServices(Type? serviceType, string? contract)

Parameters

serviceType Type

The object type.

contract string

A value which will retrieve only objects registered with the same contract.

Returns

IEnumerable<object>

A sequence of instances of the requested serviceType. The sequence should be empty (not null) if no objects of the given type are available.

GetServices<T>()

Gets all instances of the given T. Must return an empty collection if the service is not available (must not return null or throw).

public IEnumerable<T> GetServices<T>()

Returns

IEnumerable<T>

A sequence of instances of the requested T. The sequence should be empty (not null) if no objects of the given type are available.

Type Parameters

T

The object type.

GetServices<T>(string?)

Gets all instances of the given T. Must return an empty collection if the service is not available (must not return null or throw).

public IEnumerable<T> GetServices<T>(string? contract)

Parameters

contract string

A value which will retrieve only objects registered with the same contract.

Returns

IEnumerable<T>

A sequence of instances of the requested T. The sequence should be empty (not null) if no objects of the given type are available.

Type Parameters

T

The object type.

HasRegistration(Type?)

Determines whether a registration exists for the specified service type.

public virtual bool HasRegistration(Type? serviceType)

Parameters

serviceType Type

The type of the service to check for registration. Can be null to indicate an unspecified service type.

Returns

bool

true if a registration exists for the specified service type; otherwise, false.

HasRegistration(Type?, string?)

Determines whether a registration exists for the specified service type and contract.

public virtual bool HasRegistration(Type? serviceType, string? contract)

Parameters

serviceType Type

The type of the service to check for registration. Can be null to indicate a default or unspecified service type, depending on the implementation.

contract string

An optional contract name that distinguishes between multiple registrations of the same service type. Can be null or empty to indicate the default contract.

Returns

bool

true if a registration exists for the specified service type and contract; otherwise, false.

HasRegistration<T>()

Determines whether a registration exists for the specified service type.

public bool HasRegistration<T>()

Returns

bool

true if a registration for the specified service type exists; otherwise, false.

Type Parameters

T

The type of the service to check for registration.

HasRegistration<T>(string?)

Determines whether a registration exists for the specified service type and contract.

public bool HasRegistration<T>(string? contract)

Parameters

contract string

An optional contract name that identifies a specific registration. Can be null to check for the default registration.

Returns

bool

true if a registration exists for the specified service type and contract; otherwise, false.

Type Parameters

T

The service type to check for a registration.

Register(Func<object?>, Type?)

Registers a factory method for creating instances of the specified service type.

public virtual void Register(Func<object?> factory, Type? serviceType)

Parameters

factory Func<object>

A delegate that returns an instance of the service. The delegate may return null if appropriate for the service.

serviceType Type

The type of the service to register. If null, the type may be inferred from the factory's return type.

Remarks

Use this method to provide custom logic for creating service instances. If serviceType is null, the registration mechanism may attempt to infer the service type from the factory delegate's return type.

Register(Func<object?>, Type?, string?)

Registers a factory method for creating instances of a specified service type and contract.

public virtual void Register(Func<object?> factory, Type? serviceType, string? contract)

Parameters

factory Func<object>

A delegate that returns an instance of the service to register. Cannot be null.

serviceType Type

The type of the service to register. If null, the type is inferred from the factory's return value.

contract string

An optional contract name that distinguishes this registration from others of the same service type. Can be null or empty for the default contract.

Remarks

Use this method to register services with custom creation logic, such as when dependencies or configuration are required at instantiation. If multiple registrations exist for the same service type and contract, the most recent registration may take precedence, depending on the container's behavior.

RegisterConstant<T>(T?)

Registers a constant value of the specified reference type for later retrieval or use.

public void RegisterConstant<T>(T? value) where T : class

Parameters

value T

The constant value to register. Can be null to represent the absence of a value.

Type Parameters

T

The reference type of the constant value to register.

RegisterConstant<T>(T?, string?)

Registers a constant instance of the specified type for use in dependency resolution.

public void RegisterConstant<T>(T? value, string? contract) where T : class

Parameters

value T

The constant instance to register. Can be null if null values are supported by the container.

contract string

An optional contract name that uniquely identifies the registration. Can be null to register without a contract.

Type Parameters

T

The type of the constant instance to register. Must be a reference type.

Remarks

Registering a constant ensures that the same instance is returned for all requests matching the specified type and contract. If a contract is provided, the constant is associated only with that contract; otherwise, it is registered for the type without a contract.

RegisterLazySingleton<T>(Func<T?>)

Registers a singleton service of type T that is created lazily using the specified factory function.

public void RegisterLazySingleton<T>(Func<T?> valueFactory) where T : class

Parameters

valueFactory Func<T>

A function that provides the instance of type T when the service is first requested. The function may return null if no instance should be registered.

Type Parameters

T

The type of the service to register. Must be a reference type with a public parameterless constructor.

Remarks

The service instance is not created until it is first requested. Subsequent requests will return the same instance. Registering multiple lazy singletons of the same type may result in only the first registration being used, depending on the container's behavior.

RegisterLazySingleton<T>(Func<T?>, string?)

Registers a singleton service of type T that is created lazily using the specified factory method.

public void RegisterLazySingleton<T>(Func<T?> valueFactory, string? contract) where T : class

Parameters

valueFactory Func<T>

A function that provides the instance of T when the singleton is first requested. May return null if a null singleton is desired.

contract string

An optional contract name used to distinguish between multiple registrations of the same service type. If null, the default contract is used.

Type Parameters

T

The type of the service to register. Must be a reference type with a public parameterless constructor.

Remarks

The singleton instance is not created until it is first requested. Subsequent requests for the service will return the same instance. Registering multiple lazy singletons with the same contract will overwrite previous registrations.

Register<T>(Func<T?>)

Register a function with the resolver which will generate an object for the specified service type. Most implementations will use a stack based approach to allow for multiple items to be registered.

public void Register<T>(Func<T?> factory)

Parameters

factory Func<T>

The factory function which generates our object.

Type Parameters

T

The type which is used for the registration.

Remarks

This generic method is preferred over the non-generic Register(Func<object?>, Type?) method for better performance and type safety. It enables optimizations in resolvers like GlobalGenericFirstDependencyResolver which use static generic containers for zero-cost service resolution.

Register<T>(Func<T?>, string?)

Register a function with the resolver which will generate an object for the specified service type. Optionally a contract can be registered which will indicate that registration will only work with that contract. Most implementations will use a stack based approach to allow for multiple items to be registered.

public void Register<T>(Func<T?> factory, string? contract)

Parameters

factory Func<T>

The factory function which generates our object.

contract string

A contract value which will indicates to only generate the value if this contract is specified.

Type Parameters

T

The type which is used for the registration.

Remarks

This generic method is preferred over the non-generic Register(Func<object?>, Type?, string?) method for better performance and type safety. It enables optimizations in resolvers like GlobalGenericFirstDependencyResolver which use static generic containers for zero-cost service resolution.

Register<TService, TImplementation>()

Registers a service type and its implementation for dependency resolution.

public void Register<TService, TImplementation>() where TService : class where TImplementation : class, TService, new()

Type Parameters

TService

The interface or base class type to register as a service. Must be a reference type.

TImplementation

The concrete implementation type to instantiate when resolving the service. Must be a reference type, implement TService, and have a public parameterless constructor.

Remarks

Subsequent requests for TService will resolve to instances of TImplementation. If the service type is already registered, this method may overwrite the existing registration depending on the implementation.

Register<TService, TImplementation>(string?)

Registers a service implementation with an optional contract name for dependency resolution.

public void Register<TService, TImplementation>(string? contract) where TService : class where TImplementation : class, TService, new()

Parameters

contract string

An optional contract name that distinguishes this registration from others of the same service type. Specify null to register the implementation without a contract.

Type Parameters

TService

The type of the service to register. Must be a reference type.

TImplementation

The concrete implementation type to register for the service. Must be a reference type with a public parameterless constructor.

Remarks

Use this method to associate a service interface or base class with a specific implementation, optionally under a contract name. This enables resolving different implementations of the same service type by contract. If multiple implementations are registered for the same service and contract, the behavior may depend on the container's resolution strategy.

ServiceRegistrationCallback(Type, Action<IDisposable>)

Registers a callback to be invoked when a service of the specified type is registered or becomes available.

public virtual IDisposable ServiceRegistrationCallback(Type serviceType, Action<IDisposable> callback)

Parameters

serviceType Type

The type of the service to monitor for registration. Cannot be null.

callback Action<IDisposable>

The action to invoke when the service is registered. The callback receives an IDisposable representing the service registration. Cannot be null.

Returns

IDisposable

An IDisposable that can be used to unregister the callback.

ServiceRegistrationCallback(Type, string?, Action<IDisposable>)

Registers a callback to be invoked when a service of the specified type and contract is registered or unregistered.

public virtual IDisposable ServiceRegistrationCallback(Type serviceType, string? contract, Action<IDisposable> callback)

Parameters

serviceType Type

The type of the service to monitor for registration events. Cannot be null.

contract string

An optional contract name that further qualifies the service type. May be null to match any contract.

callback Action<IDisposable>

An action to invoke when the service registration changes. The callback receives an IDisposable representing the registration. Cannot be null.

Returns

IDisposable

An IDisposable that can be disposed to unregister the callback.

Remarks

The callback is invoked whenever a matching service is registered or unregistered. Disposing the returned IDisposable will stop further notifications.

ServiceRegistrationCallback<T>(Action<IDisposable>)

Registers a callback to be invoked when a service of type T is registered, and returns a disposable object that can be used to unregister the callback.

public IDisposable ServiceRegistrationCallback<T>(Action<IDisposable> callback)

Parameters

callback Action<IDisposable>

The action to invoke when a service of type T is registered. The callback receives an IDisposable that can be used to unregister the callback.

Returns

IDisposable

An IDisposable that, when disposed, unregisters the callback.

Type Parameters

T

The type of the service to monitor for registration events.

ServiceRegistrationCallback<T>(string?, Action<IDisposable>)

Registers a callback to be invoked when a service of type T is registered under the specified contract.

public IDisposable ServiceRegistrationCallback<T>(string? contract, Action<IDisposable> callback)

Parameters

contract string

The contract name to filter service registrations. If null, the callback is invoked for all contracts.

callback Action<IDisposable>

The action to invoke when a matching service is registered. Receives an IDisposable that can be used to unregister the callback.

Returns

IDisposable

An IDisposable that, when disposed, unregisters the callback.

Type Parameters

T

The type of the service to monitor for registration.

Remarks

Use this method to observe dynamic service registrations and perform actions when services become available. The callback is invoked each time a matching service is registered. Disposing the returned IDisposable will stop further notifications.

UnregisterAll(Type?)

Unregisters all service registrations for the specified service type.

public virtual void UnregisterAll(Type? serviceType)

Parameters

serviceType Type

The type of service for which to remove all registrations. If null, all service registrations are removed.

UnregisterAll(Type?, string?)

Unregisters all the values associated with the specified type and contract - or - If the container has already been built, removes the specified contract (scope) entirely, ignoring the serviceType argument.

public virtual void UnregisterAll(Type? serviceType, string? contract)

Parameters

serviceType Type

The service type to unregister.

contract string

A value which will remove only objects registered with the same contract.

UnregisterAll<T>()

Unregisters all instances of the specified type from the registry or container.

public void UnregisterAll<T>()

Type Parameters

T

The type of objects to unregister.

Remarks

After calling this method, no instances of type T will remain registered. Subsequent requests for T may fail or result in new registrations, depending on the container's behavior.

UnregisterAll<T>(string?)

Unregisters all instances of the specified type that are associated with the given contract.

public void UnregisterAll<T>(string? contract)

Parameters

contract string

The contract name used to identify the registrations to remove. Can be null to target registrations without a contract.

Type Parameters

T

The type of the instances to unregister.

UnregisterCurrent(Type?)

Unregisters the current instance of the specified service type from the context.

public virtual void UnregisterCurrent(Type? serviceType)

Parameters

serviceType Type

The type of the service to unregister. If null, the default service type is used.

Remarks

If no instance of the specified service type is registered, this method has no effect.

UnregisterCurrent(Type?, string?)

Unregisters the current instance of the specified service type and contract from the container.

public virtual void UnregisterCurrent(Type? serviceType, string? contract)

Parameters

serviceType Type

The type of the service to unregister. If null, the default service type is used.

contract string

An optional contract name that identifies the registration to remove. If null, the default contract is used.

UnregisterCurrent<T>()

Unregisters the current instance of the specified type from the context.

public void UnregisterCurrent<T>()

Type Parameters

T

The type of the instance to unregister from the current context.

UnregisterCurrent<T>(string?)

Unregisters the current instance of type T associated with the specified contract, if any.

public void UnregisterCurrent<T>(string? contract)

Parameters

contract string

An optional contract name that specifies which registration to remove. If null, the default registration for type T is unregistered.

Type Parameters

T

The type of the instance to unregister.

Remarks

If no instance of type T is registered with the specified contract, this method has no effect.

UpdateContainer(IServiceCollection)

Updates this instance with a collection of configured services.

public void UpdateContainer(IServiceCollection services)

Parameters

services IServiceCollection

An instance of IServiceCollection.

UpdateContainer(IServiceProvider)

Updates this instance with a configured service Provider.

public void UpdateContainer(IServiceProvider serviceProvider)

Parameters

serviceProvider IServiceProvider

A ready to use service provider.