Table of Contents

Class AutofacDependencyResolver

Namespace
Splat.Autofac
Assembly
Splat.Autofac.dll

Provides an Autofac-based implementation of the IDependencyResolver interface for resolving services and managing registrations within ReactiveUI applications.

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

Remarks

This resolver integrates Autofac's container and lifetime scope management with ReactiveUI's dependency resolution system. It is designed to support the initialization and service resolution needs of ReactiveUI, including contract-based and generic service lookups. Due to Autofac 5+ containers being immutable, registration and unregistration methods are intended for internal use during initialization and should not be used by end-users after the container is built. To override default registrations, register your services after calling InitializeReactiveUI. Thread safety is ensured for all public operations. Disposing the resolver will release Autofac container resources.

Constructors

AutofacDependencyResolver(ContainerBuilder)

Initializes a new instance of the AutofacDependencyResolver class.

public AutofacDependencyResolver(ContainerBuilder builder)

Parameters

builder ContainerBuilder

Autofac container builder.

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.

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 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 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?)

Important: Because Autofac 5+ containers are immutable, this method should not be used by the end-user. It is still needed to satisfy ReactiveUI initialization procedure. Register a function with the resolver which will generate a object for the specified service type. Most implementations will use a stack based approach to allow for multiple items to be registered.

[Obsolete("Because Autofac 5+ containers are immutable, this method should not be used by the end-user.")]
public virtual void Register(Func<object?> factory, Type? serviceType)

Parameters

factory Func<object>

The factory function which generates our object.

serviceType Type

The type which is used for the registration.

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

Important: Because Autofac 5+ containers are immutable, this method should not be used by the end-user. It is still needed to satisfy ReactiveUI initialization procedure. Register a function with the resolver which will generate a object for the specified service type. A contract is 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.

[Obsolete("Because Autofac 5+ containers are immutable, this method should not be used by the end-user.")]
public virtual void Register(Func<object?> factory, Type? serviceType, string? contract)

Parameters

factory Func<object>

The factory function which generates our object.

serviceType Type

The type which is used for the registration.

contract string

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

RegisterConstant<T>(T?)

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

[Obsolete("Because Autofac 5+ containers are immutable, this method should not be used by the end-user.")]
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.

[Obsolete("Because Autofac 5+ containers are immutable, this method should not be used by the end-user.")]
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.

[Obsolete("Because Autofac 5+ containers are immutable, this method should not be used by the end-user.")]
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.

[Obsolete("Because Autofac 5+ containers are immutable, this method should not be used by the end-user.")]
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.

[Obsolete("Because Autofac 5+ containers are immutable, this method should not be used by the end-user.")]
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.

[Obsolete("Because Autofac 5+ containers are immutable, this method should not be used by the end-user.")]
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.

[Obsolete("Because Autofac 5+ containers are immutable, this method should not be used by the end-user.")]
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.

[Obsolete("Because Autofac 5+ containers are immutable, this method should not be used by the end-user.")]
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.

SetLifetimeScope(ILifetimeScope)

Sets the lifetime scope which will be used to resolve ReactiveUI services. It should be set after Autofac application-wide container is built.

public void SetLifetimeScope(ILifetimeScope lifetimeScope)

Parameters

lifetimeScope ILifetimeScope

Lifetime scope, which will be used to resolve ReactiveUI services.

UnregisterAll(Type?)

Because Autofac 5+ containers are immutable, UnregisterAll method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it.

[Obsolete("Because Autofac 5+ containers are immutable, UnregisterAll method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it https://autofaccn.readthedocs.io/en/latest/register/registration.html#default-registrations.")]
public virtual void UnregisterAll(Type? serviceType)

Parameters

serviceType Type

The service type to unregister.

Exceptions

NotImplementedException

This is not implemented by default.

UnregisterAll(Type?, string?)

Because Autofac 5+ containers are immutable, UnregisterAll method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it.

[Obsolete("Because Autofac 5+ containers are immutable, UnregisterAll method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it https://autofaccn.readthedocs.io/en/latest/register/registration.html#default-registrations.")]
public virtual void UnregisterAll(Type? serviceType, string? contract)

Parameters

serviceType Type

The service type to unregister.

contract string

The optional contract value, which will only remove the value associated with the contract.

Exceptions

NotImplementedException

This is not implemented by default.

UnregisterAll<T>()

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

[Obsolete("Because Autofac 5+ containers are immutable, UnregisterAll method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it https://autofaccn.readthedocs.io/en/latest/register/registration.html#default-registrations.")]
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.

[Obsolete("Because Autofac 5+ containers are immutable, UnregisterAll method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it https://autofaccn.readthedocs.io/en/latest/register/registration.html#default-registrations.")]
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?)

Because Autofac 5+ containers are immutable, UnregisterCurrent method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it.

[Obsolete("Because Autofac 5+ containers are immutable, UnregisterCurrent method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it https://autofaccn.readthedocs.io/en/latest/register/registration.html#default-registrations.")]
public virtual void UnregisterCurrent(Type? serviceType)

Parameters

serviceType Type

The service type to unregister.

Remarks

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

Exceptions

NotImplementedException

This is not implemented by default.

UnregisterCurrent(Type?, string?)

Because Autofac 5+ containers are immutable, UnregisterCurrent method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it.

[Obsolete("Because Autofac 5+ containers are immutable, UnregisterCurrent method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it https://autofaccn.readthedocs.io/en/latest/register/registration.html#default-registrations.")]
public virtual void UnregisterCurrent(Type? serviceType, string? contract)

Parameters

serviceType Type

The service type to unregister.

contract string

The optional contract value, which will only remove the value associated with the contract.

Exceptions

NotImplementedException

This is not implemented by default.

UnregisterCurrent<T>()

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

[Obsolete("Because Autofac 5+ containers are immutable, UnregisterCurrent method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it https://autofaccn.readthedocs.io/en/latest/register/registration.html#default-registrations.")]
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.

[Obsolete("Because Autofac 5+ containers are immutable, UnregisterCurrent method is not available anymore. Instead, simply register your service after InitializeReactiveUI to override it https://autofaccn.readthedocs.io/en/latest/register/registration.html#default-registrations.")]
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.