Example #1
0
 public ExpressionsServiceProviderEngine(
     IEnumerable <ServiceDescriptor> serviceDescriptors,
     IServiceProviderEngineCallback callback,
     IInterceptingProxyFactory interceptingProxyFactory) : base(serviceDescriptors, callback, interceptingProxyFactory)
 {
     _expressionResolverBuilder = new ExpressionResolverBuilder(RuntimeResolver, this, Root);
 }
Example #2
0
 public InterceptionCallSite(IInterceptingProxyFactory proxyFactory, IServiceCallSite targetCallSite)
 {
     this.ProxyFactory       = proxyFactory;
     this.TargetCallSite     = targetCallSite;
     this.ServiceType        = targetCallSite.ServiceType;
     this.ImplementationType = targetCallSite.ImplementationType;
 }
Example #3
0
        /// <summary>
        /// Create a new <see cref="Interceptable{T}"/>
        /// </summary>
        /// <param name="proxyFactory">The service factory to create the proxy to wrapping the target service instance.</param>
        /// <param name="serviceProvider">The service provider to provide target service instances.</param>
        public Interceptable(IInterceptingProxyFactory proxyFactory, IServiceProvider serviceProvider)
        {
            Guard.ArgumentNotNull(proxyFactory, nameof(proxyFactory));
            Guard.ArgumentNotNull(serviceProvider, nameof(serviceProvider));

            _proxyFactory    = proxyFactory;
            _serviceProvider = serviceProvider;
        }
Example #4
0
 public InterceptionCallSite(IInterceptingProxyFactory proxyFactory, IServiceCallSite targetCallSite)
 {
     ProxyFactory       = proxyFactory;
     TargetCallSite     = targetCallSite;
     ServiceType        = targetCallSite.ServiceType;
     ImplementationType = targetCallSite.ImplementationType;
     CanIntercept       = ServiceType.IsInterface || (targetCallSite.Kind != CallSiteKind.Factory && targetCallSite.Kind != CallSiteKind.Constant);
 }
Example #5
0
        public Interceptable(IInterceptingProxyFactory proxyFactory, IServiceProvider serviceProvider, IInterceptableServiceProviderIndicator indicator)
        {
            Guard.ArgumentNotNull(proxyFactory, nameof(proxyFactory));
            Guard.ArgumentNotNull(serviceProvider, nameof(serviceProvider));

            _proxyFactory    = proxyFactory;
            _serviceProvider = serviceProvider;
            _isInterceptableServiceProvider = indicator.IsInterceptable;
        }
Example #6
0
        /// <summary>
        /// Create the specified type of proxy.
        /// </summary>
        /// <typeparam name="T">The type of proxy.</typeparam>
        /// <param name="proxyFactory">The proxy factory.</param>
        /// <param name="serviceProvider">The service provider.</param>
        /// <returns>The interceptable proxy.</returns>
        /// <exception cref="ArgumentException">Invalid ServiceProvider - serviceProvider</exception>
        /// <exception cref="ArgumentNullException">Specified <paramref name="proxyFactory" /> is null.</exception>
        /// <exception cref="ArgumentNullException">Specified <paramref name="proxyFactory" /> is null.</exception>
        public static T Create <T>(this IInterceptingProxyFactory proxyFactory, IServiceProvider serviceProvider)
        {
            Guard.ArgumentNotNull(proxyFactory, nameof(proxyFactory));
            Guard.ArgumentNotNull(serviceProvider, nameof(serviceProvider));

            if (serviceProvider is ServiceProvider2)
            {
                throw new ArgumentException("Invalid ServiceProvider", nameof(serviceProvider));
            }

            return((T)proxyFactory.Create(typeof(T), serviceProvider));
        }
Example #7
0
 protected ServiceProviderEngine(
     IEnumerable <ServiceDescriptor> serviceDescriptors,
     IServiceProviderEngineCallback callback,
     IInterceptingProxyFactory interceptingProxyFactory)
 {
     _createServiceAccessor = CreateServiceAccessor;
     _callback       = callback;
     Root            = new ServiceProviderEngineScope(this);
     RuntimeResolver = new CallSiteRuntimeResolver();
     CallSiteFactory = new CallSiteFactory(serviceDescriptors, interceptingProxyFactory);
     CallSiteFactory.Add(typeof(IServiceProvider), new ServiceProviderCallSite());
     CallSiteFactory.Add(typeof(IServiceScopeFactory), new ServiceScopeFactoryCallSite());
     RealizedServices = new ConcurrentDictionary <Type, Func <ServiceProviderEngineScope, object> >();
 }
Example #8
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;
        }
Example #9
0
 /// <summary>
 /// Create the specified type of proxy to wrap the specified target instance.
 /// </summary>
 /// <typeparam name="T">The type of proxy.</typeparam>
 /// <param name="proxyFactory">The proxy factory.</param>
 /// <param name="target">The target object wrapped by the created proxy.</param>
 /// <returns>The proxy wrapping the specified target.</returns>
 /// <exception cref="ArgumentNullException">Specified <paramref name="proxyFactory"/> is null.</exception>
 /// <exception cref="ArgumentNullException">Specified <paramref name="target"/> is null.</exception>
 public static T Wrap <T>(this IInterceptingProxyFactory proxyFactory, T target) where T : class
 {
     Guard.ArgumentNotNull(proxyFactory, nameof(proxyFactory));
     Guard.ArgumentNotNull(target, nameof(target));
     return((T)proxyFactory.Wrap(typeof(T), target));
 }
        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);
        }
Example #11
0
 public CallSiteFactory(IEnumerable <ServiceDescriptor> descriptors, IInterceptingProxyFactory interceptingProxyFactory)
 {
     _interceptingProxyFactory = interceptingProxyFactory;
     _descriptors = descriptors.ToList();
     Populate(descriptors);
 }
        private static void OverrideRegistrations(IServiceCollection services, IInterceptorResolver resolver, IInterceptingProxyFactory proxyFactory)
        {
            services.TryAddSingleton <ServiceOverrideIndicator, ServiceOverrideIndicator>();
            var result = (from it in services
                          let implType = it.ImplementationType
                                         let interceptors = implType == null
                                ? null
                                : resolver.GetInterceptors(it.ImplementationType)
                                                            where !it.ServiceType.IsInterface &&
                                                            it.ImplementationFactory == null &&
                                                            it.ImplementationInstance == null &&
                                                            it.ImplementationType != null &&
                                                            !interceptors.IsEmpty
                                                            select new { ServiceDescriptor = it, Interceptors = interceptors })
                         .ToArray();

            Array.ForEach(result.Select(it => it.ServiceDescriptor).ToArray(), it => services.Remove(it));
            foreach (var item in result.ToArray())
            {
                var serviceType   = item.ServiceDescriptor.ServiceType;
                var newDescriptor = new ServiceDescriptor(serviceType, _ => proxyFactory.Create(item.ServiceDescriptor.ImplementationType, _), item.ServiceDescriptor.Lifetime);
                services.Add(newDescriptor);
            }
        }