Skip to content

LifetimeMismatchExtensions

NexusLabs.Needlr

NexusLabs.Needlr

LifetimeMismatchExtensions Class

Extension methods for detecting lifetime mismatches (captive dependencies) in service registrations.

public static class LifetimeMismatchExtensions

Inheritance System.Object 🡒 LifetimeMismatchExtensions

Methods

LifetimeMismatchExtensions.DetectLifetimeMismatches(this IServiceCollection) Method

Detects lifetime mismatches (captive dependencies) in the service collection. A lifetime mismatch occurs when a longer-lived service depends on a shorter-lived service.

public static System.Collections.Generic.IReadOnlyList<NexusLabs.Needlr.LifetimeMismatch> DetectLifetimeMismatches(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);

Parameters

services Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection to analyze.

Returns

System.Collections.Generic.IReadOnlyList<LifetimeMismatch>
A list of detected lifetime mismatches.

Exceptions

System.ArgumentNullException
Thrown when services is null.

Remarks

Lifetime hierarchy (from longest to shortest): - Singleton (lives for entire application lifetime) - Scoped (lives for the scope/request lifetime) - Transient (new instance every time)

A mismatch occurs when a service with a longer lifetime depends on a service with a shorter lifetime. For example, a Singleton depending on a Scoped service will "capture" the scoped instance, causing it to live longer than intended.

Factory registrations cannot be analyzed and are skipped.