Class OverrideService<TService>
Dependency Injection helper to override a service type with a different implementation.
[InternalApi_DoNotUse_MayChangeWithoutNotice]
public static class OverrideService<TService> where TService : class
Type Parameters
TService
- Inheritance
-
objectOverrideService<TService>
Remarks
When using (...) this, it will ensure that any code within the using will receive the overridden implementation, while code outside will receive the original implementation.
This will only have an effect, if a service was initially registered for overriding, using the various Register<TImplementation>() methods.
Properties
CurrentFactory
Gets the current active factory delegate.
public static Func<IServiceProvider, TService>? CurrentFactory { get; }
Property Value
- Func<IServiceProvider, TService>
IsOverridden
Indicates whether an override is active in the current execution context.
public static bool IsOverridden { get; }
Property Value
OverrideCount
Gets the total number of nested overrides currently active (0 if none).
public static int OverrideCount { get; }
Property Value
OverridePath
public static string OverridePath { get; }
Property Value
Methods
Register()
Generate the factory function to either get from the override, or from the underlying service provider.
public static Func<IServiceProvider, TService> Register()
Returns
- Func<IServiceProvider, TService>
A factory function to create the service instance
Register(Func<IServiceProvider, TService>)
Register with a custom factory method
public static Func<IServiceProvider, TService> Register(Func<IServiceProvider, TService> factory)
Parameters
factoryFunc<IServiceProvider, TService>A factory function to create the service instance
Returns
- Func<IServiceProvider, TService>
A factory function to create the service instance
Register(Func<TService>)
Register with a custom factory method
public static Func<IServiceProvider, TService> Register(Func<TService> factory)
Parameters
factoryFunc<TService>A factory function to create the service instance
Returns
- Func<IServiceProvider, TService>
A factory function to create the service instance
RegisterScoped()
public static Func<IServiceProvider, TService> RegisterScoped()
Returns
- Func<IServiceProvider, TService>
RegisterScoped<TImplementation>()
Register as if it were scoped. We need this, because the true registration must be transient (otherwise the factory would never be rechecked). But we still want to be able to have the initial registration behave as scoped, which is why we do this.
public static Func<IServiceProvider, TService> RegisterScoped<TImplementation>() where TImplementation : class, TService
Returns
- Func<IServiceProvider, TService>
Type Parameters
TImplementation
Register<TImplementation>()
Generate the factory function to either get from the override, or from the underlying service provider.
public static Func<IServiceProvider, TService> Register<TImplementation>() where TImplementation : class, TService
Returns
- Func<IServiceProvider, TService>
A factory function to create the service instance
Type Parameters
TImplementationImplementation type
Use(Func<IServiceProvider, TService>, string?, string?)
Use Overload 3: Supply a custom factory function
public static IDisposable Use(Func<IServiceProvider, TService> factory, string? hint = null, string? cName = null)
Parameters
factoryFunc<IServiceProvider, TService>The factory function to use.
hintstringAn optional hint for debugging purposes.
cNamestringThe caller member name (automatic)
Returns
- IDisposable
UseIfNotOverridden(Func<IServiceProvider, TService>, string?, string?)
public static IDisposable UseIfNotOverridden(Func<IServiceProvider, TService> factory, string? hint = null, string? cName = null)
Parameters
factoryFunc<IServiceProvider, TService>hintstringcNamestring
Returns
- IDisposable
UseIfNotOverridden<TImplementation>(string?, string?)
Idempotent Begin: Only applies if no override exists
public static IDisposable UseIfNotOverridden<TImplementation>(string? hint = null, string? cName = null) where TImplementation : class, TService
Parameters
Returns
- IDisposable
Type Parameters
TImplementation
Use<TImplementation>(string?, string?)
Use Overload 1: Supply a type (preserves transient lifetime via DI container)
public static IDisposable Use<TImplementation>(string? hint = null, string? cName = null) where TImplementation : class, TService
Parameters
hintstringAn optional hint for debugging purposes.
cNamestringThe caller member name (automatic)
Returns
- IDisposable
Type Parameters
TImplementationThe type to resolve instead, must be registered with the DI container.
Use<TImplementation>(TImplementation, string?, string?)
Use Overload 2: Supply a specific instance (singleton)
public static IDisposable Use<TImplementation>(TImplementation value, string? hint = null, string? cName = null) where TImplementation : class, TService
Parameters
valueTImplementationThe specific instance to use.
hintstringAn optional hint for debugging purposes.
cNamestringThe caller member name (automatic)
Returns
- IDisposable
Type Parameters
TImplementationThe type of the instance to use.