public void ScopeDisposed(ServiceProviderEngine engine, ScopeState state)
 {
     if (IsEnabled(EventLevel.Verbose, EventKeywords.All))
     {
         ScopeDisposed(engine.GetHashCode(), state.ResolvedServicesCount, state.DisposableServicesCount);
     }
 }
Beispiel #2
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);
        }
Beispiel #3
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());
                }
            }
        }
Beispiel #4
0
        public static ServiceProvider BuildServiceProvider(this IServiceCollection services, ServiceProviderMode mode, ServiceProviderOptions options = null)
        {
            options ??= ServiceProviderOptions.Default;

            if (mode == ServiceProviderMode.Default)
            {
                return(services.BuildServiceProvider(options));
            }

            var provider = new ServiceProvider(services, ServiceProviderOptions.Default);
            ServiceProviderEngine engine = mode switch
            {
                ServiceProviderMode.Dynamic => new DynamicServiceProviderEngine(provider),
                ServiceProviderMode.Runtime => RuntimeServiceProviderEngine.Instance,
                ServiceProviderMode.Expressions => new ExpressionsServiceProviderEngine(provider),
                ServiceProviderMode.ILEmit => new ILEmitServiceProviderEngine(provider),
                _ => throw new NotSupportedException()
            };

            provider._engine = engine;
            return(provider);
        }
    }
Beispiel #5
0
 private static Func <ServiceProviderEngineScope, object> CompileCallSite(ServiceCallSite callSite, ServiceProviderEngine engine)
 {
     return(new ExpressionResolverBuilder(CallSiteRuntimeResolver, engine, engine.Root).Build(callSite));
 }
Beispiel #6
0
 private static object Invoke(IServiceCallSite callSite, ServiceProviderEngine provider)
 {
     return(CallSiteRuntimeResolver.Resolve(callSite, provider.Root));
 }
Beispiel #7
0
 public void Track(ServiceProviderEngine engine)
 {
     DependencyInjectionEventSource.Log.ScopeDisposed(engine, this);
 }
Beispiel #8
0
 public ServiceProviderEngineScope(ServiceProviderEngine engine)
 {
     Engine = engine;
 }