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)); }
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)); }
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); } }