Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="3.1.2-preview-556" />

 ScopedLifetime

Represents a scoped lifetime.
using Stashbox.Exceptions; using Stashbox.Registration; using Stashbox.Resolution; using Stashbox.Utils; using System; using System.Linq.Expressions; namespace Stashbox.Lifetime { public class ScopedLifetime : FactoryLifetimeDescriptor { protected override int LifeSpan { get; } = 10; private protected override bool StoreResultInLocalVariable => true; protected override Expression ApplyLifetime(Func<IResolutionScope, object> factory, ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, Type resolveType) { if (resolutionContext.CurrentContainerContext.ContainerConfiguration.LifetimeValidationEnabled && resolutionContext.IsRequestedFromRoot) throw new LifetimeValidationFailedException(serviceRegistration.ImplementationType, $"""{serviceRegistration.ImplementationType}""{base.Name}""" + "that would promote the service's lifetime to singleton."); return resolutionContext.CurrentScopeParameter.CallMethod(Constants.GetOrAddScopedObjectMethod, serviceRegistration.RegistrationId.AsConstant(), serviceRegistration.SynchronizationObject.AsConstant(), factory.AsConstant()).ConvertTo(resolveType); } } }