コード例 #1
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));
        }
コード例 #2
0
        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);
            }
        }