private static IServiceCollection AddInterceptionCore(this IServiceCollection services, Action <InterceptionBuilder> configure, bool overrideExistingRegistrations)
        {
            Guard.ArgumentNotNull(services, nameof(services));
            services.TryAddTransient(typeof(IInterceptable <>), typeof(Interceptable <>));
            services.TryAddSingleton <IInterceptorChainBuilder, InterceptorChainBuilder>();
            services.TryAddSingleton <IInterceptingProxyFactory, InterceptingProxyFactory>();
            services.TryAddSingleton <IInstanceDynamicProxyGenerator, InterfaceDynamicProxyGenerator>();
            services.TryAddSingleton <ITypeDynamicProxyGenerator, VirtualMethodDynamicProxyGenerator>();
            services.TryAddSingleton <IDynamicProxyFactoryCache>(new DynamicProxyFactoryCache());

            var builder = new InterceptionBuilder(services);

            configure?.Invoke(builder);
            services.AddSingleton <IInterceptorResolver>(_ => new InterceptorResolver(_.GetRequiredService <IInterceptorChainBuilder>(), builder.InterceptorProviderResolvers));

            var provider     = services.BuildServiceProvider();
            var resolver     = provider.GetRequiredService <IInterceptorResolver>();
            var proxyFactory = provider.GetRequiredService <IInterceptingProxyFactory>();

            if (overrideExistingRegistrations)
            {
                OverrideRegistrations(services, resolver, proxyFactory);
                services.AddSingleton <IInterceptableServiceProviderIndicator>(new InterceptableServiceProviderIndicator(false));
            }
            else
            {
                services.AddSingleton <IInterceptableServiceProviderIndicator>(new InterceptableServiceProviderIndicator(true));
            }
            return(services);
        }
예제 #2
0
        /// <summary>
        /// Add interception based services.
        /// </summary>
        /// <param name="services">The contract for a collection of service descriptors.</param>
        /// <param name="configure">The <see cref="Action{InterceptionBuilder}"/> used to perform more service registrations.</param>
        /// <returns>The current <see cref="IServiceCollection"/>.</returns>
        public static IServiceCollection AddInterception(this IServiceCollection services, Action <InterceptionBuilder> configure = null)
        {
            Guard.ArgumentNotNull(services, nameof(services));
            if (services.Any(it => it.ServiceType == typeof(IDuplicate)))
            {
                throw new InvalidOperationException("Duplicate invocation to AddInterception method.");
            }
            services.AddHttpContextAccessor();
            services.TryAddSingleton <IDuplicate, Duplicate>();
            services.TryAddSingleton <ICodeGeneratorFactory, CodeGeneratorFactory>();
            services.TryAddSingleton <IInterceptorChainBuilder, InterceptorChainBuilder>();
            services.TryAddSingleton <IInterceptableProxyFactoryCache, InterceptableProxyFactoryCache>();

            var builder = new InterceptionBuilder(services);

            configure?.Invoke(builder);
            services.TryAddSingleton <IInterceptorResolver>(provider =>
            {
                var chainBuilder      = provider.GetRequiredService <IInterceptorChainBuilder>();
                var providerResolvers = builder.InterceptorProviderResolvers;
                if (!providerResolvers.OfType <AttributeInterceptorProviderResolver>().Any())
                {
                    providerResolvers.Add(nameof(AttributeInterceptorProviderResolver), new AttributeInterceptorProviderResolver());
                }
                return(new InterceptorResolver(chainBuilder, providerResolvers));
            });
            return(services);
        }
 /// <summary>
 /// Registers the interceptors.
 /// </summary>
 /// <param name="builder">The <see cref="InterceptionBuilder"/>.</param>
 /// <param name="register">The <see cref="Action{IInterceptorRegistry} "/> used to register interceptors.</param>
 /// <returns>The current <see cref="InterceptionBuilder"/></returns>
 public static InterceptionBuilder RegisterInterceptors(this InterceptionBuilder builder, Action <IInterceptorRegistry> register)
 {
     if (builder == null)
     {
         throw new ArgumentNullException(nameof(builder));
     }
     if (register == null)
     {
         throw new ArgumentNullException(nameof(register));
     }
     builder.Services.Configure <InterceptionOptions>(it => it.InterceptorRegistrations = register);
     return(builder);
 }
 /// <summary>
 /// Register <see cref="DynamicProxyFactory"/>.
 /// </summary>
 /// <param name="builder">The <see cref="InterceptionBuilder"/> to which the <see cref="DynamicProxyFactory"/> is registered.</param>
 /// <returns>The <see cref="InterceptionBuilder"/> with the service registration of <see cref="DynamicProxyFactory"/> is registered.</returns>
 ///<exception cref="ArgumentNullException">The argument <paramref name="builder"/> is null.</exception>
 public static InterceptionBuilder SetDynamicProxyFactory(this InterceptionBuilder builder)
 {
     Guard.ArgumentNotNull(builder, nameof(builder));
     builder.Services.AddScoped <IProxyFactory, DynamicProxyFactory>();
     return(builder);
 }
 /// <summary>
 /// Register <see cref="DynamicProxyFactory"/>.
 /// </summary>
 /// <param name="builder">The <see cref="InterceptionBuilder"/> to which the <see cref="DynamicProxyFactory"/> is registered.</param>
 /// <returns>The <see cref="InterceptionBuilder"/> with the service registration of <see cref="DynamicProxyFactory"/> is registered.</returns>
 ///<exception cref="ArgumentNullException">The argument <paramref name="builder"/> is null.</exception>
 public static InterceptionBuilder SetCastleDynamicProxy(this InterceptionBuilder builder)
 {
     Guard.ArgumentNotNull(builder, nameof(builder));
     builder.Services.Replace(ServiceDescriptor.Singleton <IInterceptingProxyFactory, DynamicProxyFactory>());
     return(builder);
 }