Example #1
0
        public ServiceProvider(IEnumerable <ServiceDescriptor> serviceDescriptors)
        {
            if (serviceDescriptors is null)
            {
                throw new ArgumentNullException(nameof(serviceDescriptors));
            }

            _callSiteFactory           = new CallSiteFactory(serviceDescriptors);
            _expressionResolverBuilder = new ExpressionResolverBuilder(new SingletonResolverBuilder(this, _realizedSingletonServices));
        }
 protected ServiceProviderEngine(IEnumerable <ServiceDescriptor> serviceDescriptors, IServiceProviderEngineCallback callback)
 {
     _createServiceAccessor = CreateServiceAccessor;
     _callback       = callback;
     Root            = new ServiceProviderEngineScope(this);
     RuntimeResolver = new CallSiteRuntimeResolver();
     CallSiteFactory = new CallSiteFactory(serviceDescriptors);
     CallSiteFactory.Add(typeof(IServiceProvider), new ServiceProviderCallSite());
     CallSiteFactory.Add(typeof(IServiceScopeFactory), new ServiceScopeFactoryCallSite());
     RealizedServices = new ConcurrentDictionary <Type, Func <ServiceProviderEngineScope, object> >();
 }
        private Func <ServiceProviderEngineScope, object> CreateServiceAccessor(Type serviceType)
        {
            var callSite = CallSiteFactory.GetCallSite(serviceType, new CallSiteChain());

            if (callSite != null)
            {
                _callback?.OnCreate(callSite);
                return(RealizeService(callSite));
            }

            return(_ => null);
        }
Example #4
0
        protected ServiceProviderEngine(IEnumerable <ServiceDescriptor> serviceDescriptors, IServiceProviderEngineCallback callback)
        {
            _createServiceAccessor = CreateServiceAccessor;
            _callback = callback;

            Root              = new ServiceProviderEngineScope(this);
            RuntimeResolver   = new CallSiteRuntimeResolver();
            ExpressionBuilder = new CallSiteExpressionBuilder(RuntimeResolver, this, Root);
            CallSiteFactory   = new CallSiteFactory(serviceDescriptors);
            CallSiteFactory.Add(typeof(IServiceProvider), new ServiceProviderCallSite());
            CallSiteFactory.Add(typeof(IServiceScopeFactory), new ServiceScopeFactoryCallSite());
        }
        internal ServiceProvider(IEnumerable <ServiceDescriptor> serviceDescriptors, ServiceProviderOptions options)
        {
            Root = this;

            if (options.ValidateScopes)
            {
                _callSiteValidator = new CallSiteValidator();
            }

            CallSiteFactory  = new CallSiteFactory(serviceDescriptors);
            ResolvedServices = new Dictionary <object, object>();

            CallSiteFactory.Add(typeof(IServiceProvider), new ServiceProviderCallSite());
            CallSiteFactory.Add(typeof(IServiceScopeFactory), new ServiceScopeFactoryCallSite());
        }
Example #6
0
        internal object?GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
        {
            if (_disposed)
            {
                ThrowHelper.ThrowObjectDisposedException();
            }

            Func <ServiceProviderEngineScope, object?> realizedService = _realizedServices.GetOrAdd(serviceType, _createServiceAccessor);

            OnResolve(serviceType, serviceProviderEngineScope);
            DependencyInjectionEventSource.Log.ServiceResolved(this, serviceType);
            var result = realizedService.Invoke(serviceProviderEngineScope);

            System.Diagnostics.Debug.Assert(result is null || CallSiteFactory.IsService(serviceType));
            return(result);
        }
Example #7
0
        internal ServiceProvider(ICollection <ServiceDescriptor> serviceDescriptors, ServiceProviderOptions options)
        {
            // note that Root needs to be set before calling GetEngine(), because the engine may need to access Root
            Root    = new ServiceProviderEngineScope(this, isRootScope: true);
            _engine = GetEngine();
            _createServiceAccessor = CreateServiceAccessor;
            _realizedServices      = new ConcurrentDictionary <Type, Func <ServiceProviderEngineScope, object?> >();

            CallSiteFactory = new CallSiteFactory(serviceDescriptors);
            // The list of built in services that aren't part of the list of service descriptors
            // keep this in sync with CallSiteFactory.IsService
            CallSiteFactory.Add(typeof(IServiceProvider), new ServiceProviderCallSite());
            CallSiteFactory.Add(typeof(IServiceScopeFactory), new ConstantCallSite(typeof(IServiceScopeFactory), Root));
            CallSiteFactory.Add(typeof(IServiceProviderIsService), new ConstantCallSite(typeof(IServiceProviderIsService), CallSiteFactory));

            if (options.ValidateScopes)
            {
                _callSiteValidator = new CallSiteValidator();
            }

            if (options.ValidateOnBuild)
            {
                List <Exception>?exceptions = null;
                foreach (ServiceDescriptor serviceDescriptor in serviceDescriptors)
                {
                    try
                    {
                        ValidateService(serviceDescriptor);
                    }
                    catch (Exception e)
                    {
                        exceptions ??= new List <Exception>();
                        exceptions.Add(e);
                    }
                }

                if (exceptions != null)
                {
                    throw new AggregateException("Some services are not able to be constructed", exceptions.ToArray());
                }
            }

            DependencyInjectionEventSource.Log.ServiceProviderBuilt(this);
        }
Example #8
0
        private void ValidateService(ServiceDescriptor descriptor)
        {
            if (descriptor.ServiceType.IsGenericType && !descriptor.ServiceType.IsConstructedGenericType)
            {
                return;
            }

            try
            {
                ServiceCallSite?callSite = CallSiteFactory.GetCallSite(descriptor, new CallSiteChain());
                if (callSite != null)
                {
                    OnCreate(callSite);
                }
            }
            catch (Exception e)
            {
                throw new InvalidOperationException($"Error while validating the service descriptor '{descriptor}': {e.Message}", e);
            }
        }
Example #9
0
        internal ServiceProvider(IEnumerable <ServiceDescriptor> serviceDescriptors, ServiceProviderOptions options)
        {
            _engine = GetEngine();
            _createServiceAccessor = CreateServiceAccessor;
            _realizedServices      = new ConcurrentDictionary <Type, Func <ServiceProviderEngineScope, object> >();

            Root            = new ServiceProviderEngineScope(this);
            CallSiteFactory = new CallSiteFactory(serviceDescriptors);
            // The list of built in services that aren't part of the list of service descriptors
            // keep this in sync with CallSiteFactory.IsService
            CallSiteFactory.Add(typeof(IServiceProvider), new ServiceProviderCallSite());
            CallSiteFactory.Add(typeof(IServiceScopeFactory), new ServiceScopeFactoryCallSite(Root));
            CallSiteFactory.Add(typeof(IServiceProviderIsService), new ConstantCallSite(typeof(IServiceProviderIsService), CallSiteFactory));

            if (options.ValidateScopes)
            {
                _callSiteValidator = new CallSiteValidator();
            }

            if (options.ValidateOnBuild)
            {
                List <Exception> exceptions = null;
                foreach (ServiceDescriptor serviceDescriptor in serviceDescriptors)
                {
                    try
                    {
                        ValidateService(serviceDescriptor);
                    }
                    catch (Exception e)
                    {
                        exceptions = exceptions ?? new List <Exception>();
                        exceptions.Add(e);
                    }
                }

                if (exceptions != null)
                {
                    throw new AggregateException("Some services are not able to be constructed", exceptions.ToArray());
                }
            }
        }
Example #10
0
        private Func <ServiceProviderEngineScope, object?> CreateServiceAccessor(Type serviceType)
        {
            ServiceCallSite?callSite = CallSiteFactory.GetCallSite(serviceType, new CallSiteChain());

            if (callSite != null)
            {
                DependencyInjectionEventSource.Log.CallSiteBuilt(this, serviceType, callSite);
                OnCreate(callSite);

                // Optimize singleton case
                if (callSite.Cache.Location == CallSiteResultCacheLocation.Root)
                {
                    object?value = CallSiteRuntimeResolver.Instance.Resolve(callSite, Root);
                    return(scope => value);
                }

                return(_engine.RealizeService(callSite));
            }

            return(_ => null);
        }