/// <summary> /// Initializes a new instance of the <see cref="InterceptingProxyFactory"/> class. /// </summary> /// <param name="interceptorResolver">The interceptor collector.</param> /// <param name="serviceProvider">The service provider.</param> /// <exception cref="ArgumentNullException">Specified <paramref name="interceptorResolver"/> is null.</exception> /// <exception cref="ArgumentNullException">Specified <paramref name="serviceProvider"/> is null.</exception> public InterceptingProxyFactoryBase( IInterceptorResolver interceptorResolver, IServiceProvider serviceProvider) { InterceptorResolver = Guard.ArgumentNotNull(interceptorResolver, nameof(interceptorResolver)); ServiceProvider = Guard.ArgumentNotNull(serviceProvider, nameof(serviceProvider)); }
public static FluentAspectOptions UseInterceptorResolver(this FluentAspectOptions options, IInterceptorResolver resolver) { Guard.NotNull(resolver); options.InterceptorResolver = resolver; return(options); }
public InterceptableProxyFactoryCache( ICodeGeneratorFactory codeGeneratorFactory, IInterceptorResolver interceptorResolver) { _codeGeneratorFactory = codeGeneratorFactory; _interceptorResolver = interceptorResolver; _instanceFactories = new Dictionary <Tuple <Type, Type>, Func <object, object> >(); _typeFactories = new Dictionary <Type, Func <IServiceProvider, object> >(); }
/// <summary> /// Initializes a new instance of the <see cref="InterceptingProxyFactory"/> class. /// </summary> /// <param name="instanceDynamicProxyGenerator">The instance dynamic proxy generator.</param> /// <param name="typeDynamicProxyGenerator">The type dynamic proxy generator.</param> /// <param name="interceptorResolver">The interceptor collector.</param> /// <param name="serviceProvider">The service provider.</param> /// <exception cref="ArgumentNullException">Specified <paramref name="instanceDynamicProxyGenerator"/> is null.</exception> /// <exception cref="ArgumentNullException">Specified <paramref name="typeDynamicProxyGenerator"/> is null.</exception> /// <exception cref="ArgumentNullException">Specified <paramref name="interceptorResolver"/> is null.</exception> /// <exception cref="ArgumentNullException">Specified <paramref name="serviceProvider"/> is null.</exception> public InterceptingProxyFactory( IInstanceDynamicProxyGenerator instanceDynamicProxyGenerator, ITypeDynamicProxyGenerator typeDynamicProxyGenerator, IInterceptorResolver interceptorResolver, IServiceProvider serviceProvider) : base(interceptorResolver, serviceProvider) { InstanceDynamicProxyGenerator = Guard.ArgumentNotNull(instanceDynamicProxyGenerator, nameof(instanceDynamicProxyGenerator)); TypeDynamicProxyGenerator = Guard.ArgumentNotNull(typeDynamicProxyGenerator, nameof(typeDynamicProxyGenerator)); }
public ServiceDescriptorConverter( Type serviceType, Type implementationType, ServiceLifetime lifetime, IInterceptorResolver interceptorResolver, IInterceptableProxyFactoryCache factoryCache, ICodeGeneratorFactory codeGeneratorFactory) : this(new ServiceDescriptor(serviceType, implementationType, lifetime), interceptorResolver, factoryCache, codeGeneratorFactory) { }
public static FluentAspectOptions UseInterceptorResolver(this FluentAspectOptions options, IInterceptorResolver resolver) { if (null == resolver) { throw new ArgumentNullException(nameof(resolver)); } options.InterceptorResolver = resolver; return(options); }
/// <summary> /// Create a new <see cref="GenericInterceptableServiceDescriptor"/>. /// </summary> /// <param name="codeGeneratorFactory">The <see cref="ICodeGeneratorFactory"/> used to get <see cref="ICodeGenerator"/>.</param> /// <param name="interceptorResolver">The <see cref="IInterceptorResolver"/> used to resolve registered interceptors.</param> /// <param name="serviceType">The service type.</param> /// <param name="implementationType">The <see cref="Type"/> service implements.</param> /// <param name="lifetime">The <see cref="ServiceLifetime"/> of the service registration.</param> public GenericInterceptableServiceDescriptor( ICodeGeneratorFactory codeGeneratorFactory, IInterceptorResolver interceptorResolver, Type serviceType, Type implementationType, ServiceLifetime lifetime) : base(serviceType, GetInterceptableProxyType(codeGeneratorFactory, interceptorResolver, serviceType, implementationType), lifetime) { if (!serviceType.IsGenericTypeDefinition) { throw new ArgumentException("Non-open-generic type (generic type definition) is not support", nameof(serviceType)); } _targetType = implementationType; }
private static Type GetInterceptableProxyType( ICodeGeneratorFactory codeGeneratorFactory, IInterceptorResolver interceptorResolver, Type serviceType, Type implementationType) { var interceptors = serviceType.IsInterface ? interceptorResolver.GetInterceptors(serviceType, implementationType) : interceptorResolver.GetInterceptors(implementationType); var codeGenerator = codeGeneratorFactory.Create(); var context = new CodeGenerationContext(serviceType, implementationType, interceptors); var proxyType = codeGenerator.GenerateInterceptableProxyClass(context); return(proxyType); }
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); } }
/// <summary> /// Create a new <see cref="DynamicProxyFactory"/>. /// </summary> public DynamicProxyFactory(IInterceptorResolver interceptorResolver, IServiceProvider serviceProvider) : base(interceptorResolver, serviceProvider) { _proxyGenerator = new ProxyGenerator(); }
private static IServiceCollection AddInterceptableCore(this IServiceCollection services, Type serviceType, Type implementationType, ServiceLifetime lifetime, AddKind kind) { Guard.ArgumentNotNull(services, nameof(services)); Guard.ArgumentNotNull(serviceType, nameof(serviceType)); Guard.ArgumentNotNull(implementationType, nameof(implementationType)); if (serviceType.IsGenericTypeDefinition) { if (!services.Any(it => it.ServiceType == typeof(ICodeGeneratorFactory))) { throw new InvalidOperationException("IServiceCollection's AddInterception method must be called before register open generic service type."); } if (_codeGeneratorFactory == null) { var provider = services.BuildServiceProvider(); _codeGeneratorFactory = provider.GetRequiredService <ICodeGeneratorFactory>(); _interceptorResolver = provider.GetRequiredService <IInterceptorResolver>(); } if (serviceType != implementationType) { services.AddTransient(implementationType, implementationType); } } switch (kind) { case AddKind.AlwaysAdd: { if (serviceType.IsGenericTypeDefinition) { services.Add(new GenericInterceptableServiceDescriptor(_codeGeneratorFactory, _interceptorResolver, serviceType, implementationType, lifetime)); } else { services.Add(new InterceptableServiceDescriptor(serviceType, implementationType, lifetime)); } break; } case AddKind.TryAdd: { if (serviceType.IsGenericTypeDefinition) { services.TryAdd(new GenericInterceptableServiceDescriptor(_codeGeneratorFactory, _interceptorResolver, serviceType, implementationType, lifetime)); } else { services.TryAdd(new InterceptableServiceDescriptor(serviceType, implementationType, lifetime)); } break; } case AddKind.TryAddEnumerable: { if (!services.OfType <IInterceptableServiceDescriptor>().Any(it => it.TargetType == implementationType)) { if (serviceType.IsGenericTypeDefinition) { services.TryAddEnumerable(new GenericInterceptableServiceDescriptor(_codeGeneratorFactory, _interceptorResolver, serviceType, implementationType, lifetime)); } else { services.TryAddEnumerable(new InterceptableServiceDescriptor(serviceType, implementationType, lifetime)); } } break; } } return(services); }
public ServiceDescriptorConverter( ServiceDescriptor serviceDescriptor, IInterceptorResolver interceptorResolver, IInterceptableProxyFactoryCache factoryCache, ICodeGeneratorFactory codeGeneratorFactory) { Guard.ArgumentNotNull(serviceDescriptor, nameof(serviceDescriptor)); Guard.ArgumentNotNull(interceptorResolver, nameof(interceptorResolver)); Guard.ArgumentNotNull(factoryCache, nameof(factoryCache)); if (serviceDescriptor.ImplementationInstance != null || serviceDescriptor.ImplementationFactory != null || serviceDescriptor.IsInterceptable()) { _primaryDescriptor = serviceDescriptor; return; } var serviceType = serviceDescriptor.ServiceType; var implementationType = serviceDescriptor.ImplementationType; var lifetime = serviceDescriptor.Lifetime; if (serviceType.IsInterface) { var interceptors = interceptorResolver.GetInterceptors(serviceType, implementationType); if (interceptors.IsEmpty) { _primaryDescriptor = new ServiceDescriptor(serviceType, implementationType, lifetime); } else if (serviceType.IsGenericTypeDefinition) { _secondaryDescriptor = new ServiceDescriptor(implementationType, implementationType, lifetime); var codeGenerator = codeGeneratorFactory.Create(); var context = new CodeGenerationContext(serviceType, implementationType, interceptors); var proxyType = codeGenerator.GenerateInterceptableProxyClass(context); _primaryDescriptor = new ServiceDescriptor(serviceType, proxyType, lifetime); } else { _primaryDescriptor = new ServiceDescriptor(serviceType, CreateOrGet, lifetime); object CreateOrGet(IServiceProvider serviceProvider) { var target = ActivatorUtilities.CreateInstance(serviceProvider, implementationType); return(factoryCache.GetInstanceFactory(serviceType, implementationType).Invoke(target)); } } } else { var interceptors = interceptorResolver.GetInterceptors(implementationType); if (interceptors.IsEmpty) { _primaryDescriptor = new ServiceDescriptor(serviceType, implementationType, lifetime); } else { var codeGenerator = codeGeneratorFactory.Create(); var context = new CodeGenerationContext(implementationType, interceptors); var proxyType = codeGenerator.GenerateInterceptableProxyClass(context); _primaryDescriptor = new ServiceDescriptor(serviceType, proxyType, lifetime); } } }