internal IEnumerable <object> GetServices(DependencyChain chain, ContainerScope scope) { // First, read the registration from the runtime to determine the lifespan. var registrations = _registrationSource.GetRegistrations(chain.Type); return(registrations.Select(registration => { switch (registration.ServiceLifespan) { case ServiceLifespan.Transient: // For Transient, activate a new instance each time. return _backend.ActivateInstance(registration, chain, new ServiceVisitor(this, scope)); case ServiceLifespan.Singleton: // For Singleton, rely on the runtime scope to retrieve the instance. return _runtimeScope.GetScopedService(registration, chain); case ServiceLifespan.Scoped: // For Scoped, rely on the scope to retrieve the instance. return scope.GetScopedService(registration, chain); default: throw new NotImplementedException($"Lifespan '{registration.ServiceLifespan}' is not supported"); } })); }
internal object GetScopedService(ServiceRegistration registration, DependencyChain chain) { if (_disposed) { throw new ObjectDisposedException(nameof(IContainerScope)); } // Attempt to retrieve the instance from the service context. var instance = _scopeContext.GetService(registration.ConcreteType); // If instance doesn't exist, we need to activate it. if (instance == null) { // Use instance on registration, if defined. // Otherwise, activate a new instance. instance = registration.ServiceInstance ?? _backend.ActivateInstance(registration, chain, new ServiceVisitor(_runtime, this)); _scopeContext.AddService(registration.ConcreteType, instance); } return(instance); }