Beispiel #1
0
        /// <summary>
        /// Enables run-time interception support for methods and properties using dynamic proxy technique.
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection EnableDynamicProxy(this IServiceCollection services)
        {
            var proxyConfig = new DynamicProxyConfiguration(services);

            var typesInAssembly = Assembly.GetCallingAssembly().GetTypes();

            AddInterceptorMappings(proxyConfig, typesInAssembly);

            services.TryAddSingleton(proxyConfig);
            services.TryAddSingleton <IProxyGenerator, ProxyGenerator>();

            var serviceTypes = typesInAssembly
                               .Where(x => x.GetCustomAttribute <InterceptAttribute>() != null);

            var serviceProvider = services.BuildServiceProvider();

            foreach (var type in serviceTypes)
            {
                var serviceType = type.GetCustomAttribute <InterceptAttribute>().ServiceType;

                var targetObj = serviceProvider.GetRequiredService(serviceType);
                var proxyObj  = new ProxyFactory(serviceProvider, proxyConfig).CreateProxy(serviceType, targetObj);

                var serviceDescriptor = services.FirstOrDefault(x => x.ServiceType == serviceType);

                // Remove previous implementation and add the decorated service.
                services.Remove(serviceDescriptor);
                services.Add(new ServiceDescriptor(serviceType, sp => proxyObj, serviceDescriptor.Lifetime));
            }

            return(services);
        }
Beispiel #2
0
        private static void AddInterceptorMappings(DynamicProxyConfiguration proxyConfig, System.Type[] typesInAssembly)
        {
            var interceptors = typesInAssembly
                               .Where(x => x.GetCustomAttribute <InterceptForAttribute>() != null);

            foreach (var interceptor in interceptors)
            {
                var attribute = interceptor.GetCustomAttribute <InterceptForAttribute>().AttributeType;
                proxyConfig.AddInterceptor(attribute, interceptor);
            }
        }
Beispiel #3
0
 public CoreInterceptor(IServiceProvider serviceProvider, DynamicProxyConfiguration proxyConfig)
 {
     this.serviceProvider = serviceProvider;
     this.proxyConfig     = proxyConfig;
 }
Beispiel #4
0
 public ProxyFactory(IServiceProvider serviceProvider, DynamicProxyConfiguration proxyConfig)
 {
     this.serviceProvider = serviceProvider;
     this.proxyConfig     = proxyConfig;
     this.proxyGenerator  = serviceProvider.GetRequiredService <IProxyGenerator>();
 }
Beispiel #5
0
        public static IEnumerable <IMethodInterceptor> FindMethodInterceptors(this Castle.DynamicProxy.IInvocation invocation, DynamicProxyConfiguration proxyConfig, IServiceProvider serviceProvider)
        {
            var interceptors = new List <IMethodInterceptor>();

            var attributes = invocation.MethodInvocationTarget.GetCustomAttributes()
                             .Where(x => x.GetType().IsSubclassOf(typeof(MethodInterceptorAttribute)))
                             .Cast <MethodInterceptorAttribute>();

            foreach (var attribute in attributes)
            {
                var interceptorType = proxyConfig.Interceptors.FirstOrDefault(x => x.AttributeType == attribute.GetType())?.InterceptorType;

                if (interceptorType == null)
                {
                    throw new InvalidOperationException($"No suitable interceptor found for type {attribute}");
                }

                var interceptorInstance = (IMethodInterceptor)ActivatorUtilities.CreateInstance(serviceProvider, interceptorType);

                interceptors.Add(interceptorInstance);
            }

            return(interceptors);
        }