Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="5.4.2-preview-760" />

 ScopedLifetime

Represents a scoped lifetime.
using Stashbox.Exceptions; using Stashbox.Registration.ServiceRegistrations; using Stashbox.Resolution; using Stashbox.Utils; using System; using System.Linq.Expressions; using System.Runtime.CompilerServices; namespace Stashbox.Lifetime { public class ScopedLifetime : FactoryLifetimeDescriptor { protected override int LifeSpan => 10; private protected override bool StoreResultInLocalVariable => true; [System.Runtime.CompilerServices.NullableContext(1)] protected override Expression ApplyLifetime(Func<IResolutionScope, IRequestContext, 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(), factory.AsConstant(), resolutionContext.RequestContextParameter, serviceRegistration.ImplementationType.AsConstant()).ConvertTo(resolveType); } } }