Пример #1
0
        public ServiceProvider(IEnumerable <ServiceDescriptor> serviceDescriptors, ServiceProviderOptions options)
        {
            Root = this;

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

            _table = new ServiceTable(serviceDescriptors);

            _table.Add(typeof(IServiceProvider), new ServiceProviderService());
            _table.Add(typeof(IServiceScopeFactory), new ServiceScopeService());
            _table.Add(typeof(IEnumerable <>), new OpenIEnumerableService(_table));
        }
        /// <summary>
        /// Builders the interceptable service provider.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> which the service provider is built based on.</param>
        /// <param name="validateScopes">if set to <c>true</c> [validate scopes].</param>
        /// <param name="configure">The configure.</param>
        /// <returns>The interceptable service provider.</returns>
        /// <exception cref="ArgumentNullException">Specified <paramref name="services"/> is null.</exception>
        public static IServiceProvider BuildInterceptableServiceProvider(this IServiceCollection services, bool validateScopes, Action <InterceptionBuilder> configure = null)
        {
            Guard.ArgumentNotNull(services, nameof(services));
            if (services.Any(it => it.ServiceType == typeof(ServiceOverrideIndicator)))
            {
                throw new InvalidOperationException("AddInterception method cannot be called if BuildInterceptableServiceProvider method is called to create interceptable service provider.");
            }
            var options = new ServiceProviderOptions {
                ValidateScopes = validateScopes
            };

            services.AddInterceptionCore(configure, false);
            var proxyFactory = services.BuildServiceProvider().GetRequiredService <IInterceptingProxyFactory>();

            return(new InterceptableServiceProvider(services, options, proxyFactory));
        }
Пример #3
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);
        }
Пример #4
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());
                }
            }
        }
Пример #5
0
        internal ServiceProvider(IEnumerable <ServiceDescriptor> serviceDescriptors, ServiceProviderOptions options)
        {
            IServiceProviderEngineCallback callback = null;

            if (options.ValidateScopes)
            {
                callback           = this;
                _callSiteValidator = new CallSiteValidator();
            }
            switch (options.Mode)
            {
            case ServiceProviderMode.Dynamic:
                _engine = new DynamicServiceProviderEngine(serviceDescriptors, callback);
                break;

            case ServiceProviderMode.Runtime:
                _engine = new RuntimeServiceProviderEngine(serviceDescriptors, callback);
                break;

#if IL_EMIT
            case ServiceProviderMode.ILEmit:
                _engine = new ILEmitServiceProviderEngine(serviceDescriptors, callback);
                break;
#endif
            case ServiceProviderMode.Expressions:
                _engine = new ExpressionsServiceProviderEngine(serviceDescriptors, callback);
                break;

            case ServiceProviderMode.Experimental:
                _engine = new ExperimentalServiceProviderEngine(serviceDescriptors, callback);
                break;

            default:
                throw new NotSupportedException(nameof(options.Mode));
            }
        }
Пример #6
0
        internal ServiceProvider(IEnumerable <ServiceDescriptor> serviceDescriptors, ServiceProviderOptions options)
        {
            IServiceProviderEngineCallback callback = null;

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

            switch (options.Mode)
            {
            case ServiceProviderMode.Default:
#if !NETCOREAPP
                _engine = new DynamicServiceProviderEngine(serviceDescriptors, callback);
#else
                if (RuntimeFeature.IsSupported("IsDynamicCodeCompiled"))
                {
                    _engine = new DynamicServiceProviderEngine(serviceDescriptors, callback);
                }
                else
                {
                    // Don't try to compile Expressions/IL if they are going to get interpreted
                    _engine = new RuntimeServiceProviderEngine(serviceDescriptors, callback);
                }
#endif
                break;

            case ServiceProviderMode.Dynamic:
                _engine = new DynamicServiceProviderEngine(serviceDescriptors, callback);
                break;

            case ServiceProviderMode.Runtime:
                _engine = new RuntimeServiceProviderEngine(serviceDescriptors, callback);
                break;

#if IL_EMIT
            case ServiceProviderMode.ILEmit:
                _engine = new ILEmitServiceProviderEngine(serviceDescriptors, callback);
                break;
#endif
            case ServiceProviderMode.Expressions:
                _engine = new ExpressionsServiceProviderEngine(serviceDescriptors, callback);
                break;

            default:
                throw new NotSupportedException(nameof(options.Mode));
            }

            if (options.ValidateOnBuild)
            {
                List <Exception> exceptions = null;
                foreach (var serviceDescriptor in serviceDescriptors)
                {
                    try
                    {
                        _engine.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());
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Creates a <see cref="ServiceProvider"/> containing services from the provided <see cref="IServiceCollection"/>
        /// optionally enabling scope validation.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> containing service descriptors.</param>
        /// <param name="options">
        /// Configures various service provider behaviors.
        /// </param>
        /// <returns>The <see cref="ServiceProvider"/>.</returns>
        public static ServiceProvider BuildServiceProvider(this IServiceCollection services, ServiceProviderOptions options)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            IServiceProviderEngine engine;

#if !NETCOREAPP
            engine = new DynamicServiceProviderEngine(services);
#else
            if (RuntimeFeature.IsDynamicCodeCompiled)
            {
                engine = new DynamicServiceProviderEngine(services);
            }
            else
            {
                // Don't try to compile Expressions/IL if they are going to get interpreted
                engine = new RuntimeServiceProviderEngine(services);
            }
#endif

            return(new ServiceProvider(services, engine, options));
        }
Пример #8
0
        public static ServiceProvider BuildServiceProvider(this IServiceCollection services, ServiceProviderOptions options)
        {
            if (services is null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(new ServiceProvider(services, options));
        }
        internal InterceptableServiceProvider(IEnumerable <ServiceDescriptor> serviceDescriptors, ServiceProviderOptions options, IInterceptingProxyFactory interceptingProxyFactory)
        {
            IServiceProviderEngineCallback callback = null;

            if (options.ValidateScopes)
            {
                callback           = this;
                _callSiteValidator = new CallSiteValidator();
            }
            _engine = new ExpressionsServiceProviderEngine(serviceDescriptors, callback, interceptingProxyFactory);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultServiceProviderFactory"/> class
 /// with the specified <paramref name="options"/>.
 /// </summary>
 /// <param name="options">The options to use for this instance.</param>
 public DefaultServiceProviderFactory(ServiceProviderOptions options)
 {
     _options = options ?? throw new ArgumentNullException(nameof(options));
 }
Пример #11
0
        internal ServiceProvider2(IEnumerable <ServiceDescriptor> serviceDescriptors, ServiceProviderOptions options, IInterceptingProxyFactory proxyFactory)
        {
            Root = this;

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

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

            CallSiteFactory.Add(typeof(IServiceProvider), new ServiceProviderCallSite());
            CallSiteFactory.Add(typeof(IServiceScopeFactory), new ServiceScopeFactoryCallSite());

            ProxyFactory = proxyFactory;
        }
Пример #12
0
        internal ServiceProvider(IEnumerable <ServiceDescriptor> serviceDescriptors, IServiceProviderEngine engine, ServiceProviderOptions options)
        {
            _engine = engine;

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

            if (options.ValidateOnBuild)
            {
                List <Exception> exceptions = null;
                foreach (ServiceDescriptor serviceDescriptor in serviceDescriptors)
                {
                    try
                    {
                        _engine.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());
                }
            }
        }
 public static ServiceProvider BuildDynamicProxyProvider(this IServiceCollection services, ServiceProviderOptions options) => services.ToDynamicProxyService().BuildServiceProvider(options);
Пример #14
0
        /// <summary>
        /// Builds the interceptable <see cref="IServiceProvider"/>.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> containg all service registrations.</param>
        /// <param name="setup">The setup.</param>
        /// <param name="serviceProviderOptions">The options for configuring various behaviors of the default <see cref="IServiceProvider"/> implementation.</param>
        /// <returns>The built interceptable <see cref="IServiceProvider"/>.</returns>
        public static IServiceProvider BuildInterceptableServiceProvider(this IServiceCollection services, ServiceProviderOptions serviceProviderOptions, Action <InterceptionBuilder>?setup = null)
        {
            Guard.ArgumentNotNull(services);
            Guard.ArgumentNotNull(serviceProviderOptions);
            var factgory = new InterceptableServiceProviderFactory(serviceProviderOptions, setup);
            var builder  = factgory.CreateBuilder(services);

            return(builder.CreateServiceProvider());
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultServiceProviderFactory"/> class
 /// with the specified <paramref name="options"/>.
 /// </summary>
 /// <param name="options">The options to use for this instance.</param>
 public DefaultServiceProviderFactory(ServiceProviderOptions options !!)
 {