private object GetFromPool(BuiltServiceDescriptor descriptor, IContainerScope scope) { IServicePool pool = (IServicePool)descriptor.Instance; PoolServiceDescriptor pdesc = pool.GetAndLock(scope); if (pdesc == null) { throw new NullReferenceException($"{descriptor.ServiceType.ToTypeString()} Service is not registered in the container"); } if (pool.Type == ImplementationType.Scoped && scope == null) { throw new ScopeException($"{descriptor.ServiceType.ToTypeString()} is registered as Scoped but scope parameter is null for IServiceContainer.Get method"); } if (scope != null) { scope.UsePoolItem(pool, pdesc); } return(pdesc.GetInstance()); }
private async Task <object> Get(ServiceDescriptor descriptor, IContainerScope scope = null) { if (descriptor.IsPool) { IServicePool pool = (IServicePool)descriptor.Instance; PoolServiceDescriptor pdesc = await pool.GetAndLock(scope); if (pdesc == null) { throw new NullReferenceException("Could not get service from container"); } if (pool.Type == ImplementationType.Scoped && scope == null) { throw new InvalidOperationException("Type is registered as Scoped but scope parameter is null for IServiceContainer.Get method"); } if (scope != null) { scope.UsePoolItem(pool, pdesc); } return(pdesc.GetInstance()); } switch (descriptor.Implementation) { //create new instance case ImplementationType.Transient: object transient = await CreateInstance(descriptor.ImplementationType, scope); if (descriptor.AfterCreatedMethod != null) { descriptor.AfterCreatedMethod.DynamicInvoke(transient); } if (descriptor.ProxyType != null) { IServiceProxy p = (IServiceProxy) await CreateInstance(descriptor.ProxyType, scope); return(p.Proxy(transient)); } return(transient); case ImplementationType.Scoped: if (scope == null) { throw new InvalidOperationException("Type is registered as Scoped but scope parameter is null for IServiceContainer.Get method"); } return(await scope.Get(descriptor, this)); case ImplementationType.Singleton: //if instance already created return if (descriptor.Instance != null) { return(descriptor.Instance); } //create instance for first time and set Instance property of descriptor to prevent re-create for next times object instance = await CreateInstance(descriptor.ImplementationType, scope); if (descriptor.AfterCreatedMethod != null) { descriptor.AfterCreatedMethod.DynamicInvoke(instance); } if (descriptor.ProxyType != null) { IServiceProxy p = (IServiceProxy) await CreateInstance(descriptor.ProxyType, scope); object proxyObject = p.Proxy(instance); descriptor.Instance = proxyObject; } else { descriptor.Instance = instance; } return(descriptor.Instance); default: return(null); } }