コード例 #1
0
        internal object GetWithDescriptor(BuiltServiceDescriptor descriptor, IContainerScope scope)
        {
            if (descriptor.IsPool)
            {
                return(GetFromPool(descriptor, scope));
            }

            if (descriptor.Implementation == ImplementationType.Scoped && scope == null)
            {
                throw new ScopeException($"{descriptor.ServiceType.ToTypeString()} is registered as scoped service but trying to create instance when scope is null");
            }

            object service;

            if (scope != null && descriptor.Implementation == ImplementationType.Scoped)
            {
                service = scope.Get(descriptor.ServiceType);
            }
            else
            {
                service = descriptor.CreateInstance(this, scope);
            }

            return(ApplyAfterGet(descriptor, service));
        }
コード例 #2
0
        internal object Get(Type serviceType, bool executedFromScope, IContainerScope scope = null)
        {
            //throw new NullReferenceException($"Could not get service from container: {typeof(TService).ToTypeString()}");
            //throw new KeyNotFoundException($"Service type is not found: {serviceType.ToTypeString()}");

            BuiltServiceDescriptor descriptor = _services[serviceType];

            if (descriptor.IsPool)
            {
                return(GetFromPool(descriptor, scope));
            }

            if (descriptor.Implementation == ImplementationType.Scoped && scope == null)
            {
                throw new ScopeException($"{serviceType.ToTypeString()} is registered as scoped service but trying to create instance when scope is null");
            }

            if (descriptor.Instance != null)
            {
                return(ApplyAfterGet(descriptor, descriptor.Instance));
            }

            object service;

            if (!executedFromScope && scope != null && descriptor.Implementation == ImplementationType.Scoped)
            {
                service = scope.Get(serviceType);
            }
            else
            {
                service = descriptor.CreateInstance(this, scope);
            }

            return(ApplyAfterGet(descriptor, service));
        }