public static IServiceContainer BuildFluentAspectsContainer(this IServiceContainerBuilder serviceCollection, Action <FluentAspectOptions> optionsAction, Action <IFluentAspectsServiceContainerBuilder> aspectBuildAction = null, Expression <Func <Type, bool> > ignoreTypesFilter = null) { var services = new ServiceContainerBuilder(); var aspectBuilder = null != optionsAction ? serviceCollection.AddFluentAspects(optionsAction) : serviceCollection.AddFluentAspects(); aspectBuildAction?.Invoke(aspectBuilder); Expression <Func <Type, bool> > ignoreTypesExpression = t => "WeihanLi.Common.Aspect".Equals(t.Namespace); if (null != ignoreTypesFilter) { ignoreTypesExpression = ignoreTypesExpression.Or(ignoreTypesFilter); } var ignoreTypesPredicate = ignoreTypesExpression.Compile(); using (var serviceProvider = serviceCollection.Build()) { var proxyTypeFactory = serviceProvider.ResolveRequiredService <IProxyTypeFactory>(); foreach (var descriptor in serviceCollection) { if (descriptor.ServiceType.IsSealed || descriptor.ServiceType.IsNotPublic || descriptor.ServiceType.IsGenericTypeDefinition ) { services.Add(descriptor); continue; } if (ignoreTypesPredicate(descriptor.ServiceType)) { services.Add(descriptor); continue; } if (descriptor.ImplementType != null) { if (descriptor.ImplementType.IsNotPublic || descriptor.ImplementType.IsProxyType() ) { services.Add(descriptor); continue; } if (descriptor.ServiceType.IsClass && descriptor.ImplementType.IsSealed) { services.Add(descriptor); continue; } if (descriptor.ServiceType.IsGenericTypeDefinition || descriptor.ImplementType.IsGenericTypeDefinition) { var proxyType = proxyTypeFactory.CreateProxyType(descriptor.ServiceType, descriptor.ImplementType); services.Add(new ServiceDefinition(descriptor.ServiceType, proxyType, descriptor.ServiceLifetime)); continue; } } Func <IServiceProvider, object> serviceFactory = null; if (descriptor.ImplementationInstance != null) { if (descriptor.ImplementationInstance.GetType().IsPublic) { serviceFactory = provider => provider.ResolveRequiredService <IProxyFactory>() .CreateProxyWithTarget(descriptor.ServiceType, descriptor.ImplementationInstance); } } else if (descriptor.ImplementType != null) { serviceFactory = provider => { var proxy = provider.ResolveRequiredService <IProxyFactory>() .CreateProxy(descriptor.ServiceType, descriptor.ImplementType); return(proxy); }; } else if (descriptor.ImplementationFactory != null) { serviceFactory = provider => { var implement = descriptor.ImplementationFactory(provider); if (implement == null) { return(null); } var implementType = implement.GetType(); if (implementType.IsNotPublic || implementType.IsProxyType()) { return(implement); } return(provider.ResolveRequiredService <IProxyFactory>() .CreateProxyWithTarget(descriptor.ServiceType, implement)); }; } if (null != serviceFactory) { services.Add(new ServiceDefinition(descriptor.ServiceType, serviceFactory, descriptor.ServiceLifetime)); } else { services.Add(descriptor); } } } var container = services.Build(); DependencyResolver.SetDependencyResolver(container); return(container); }
public static IServiceContainer BuildFluentAspectsContainer(this IServiceContainerBuilder serviceCollection, Action <FluentAspectOptions> optionsAction, Action <IFluentAspectsServiceContainerBuilder> aspectBuildAction = null, Func <Type, bool> ignoreTypesPredict = null) { var services = new ServiceContainerBuilder(); var aspectBuilder = null != optionsAction ? services.AddFluentAspects(optionsAction) : services.AddFluentAspects(); aspectBuildAction?.Invoke(aspectBuilder); foreach (var descriptor in serviceCollection) { if (ignoreTypesPredict?.Invoke(descriptor.ServiceType) == true) { services.Add(descriptor); continue; } if (descriptor.ServiceType.IsSealed || descriptor.ImplementType.IsProxyType() || (descriptor.ServiceType.IsClass && (descriptor.ImplementType?.IsSealed == true || descriptor.ImplementType?.IsPublic == false))) { services.Add(descriptor); } else { Func <IServiceProvider, object> serviceFactory = null; if (descriptor.ImplementationInstance != null) { serviceFactory = provider => provider.ResolveRequiredService <IProxyFactory>() .CreateProxyWithTarget(descriptor.ServiceType, descriptor.ImplementationInstance); } else if (descriptor.ImplementationFactory != null) { serviceFactory = provider => { var implement = descriptor.ImplementationFactory(provider); if (implement?.GetType().IsProxyType() == true) { return(implement); } return(provider.ResolveRequiredService <IProxyFactory>() .CreateProxyWithTarget(descriptor.ServiceType, implement)); }; } else if (descriptor.ImplementType != null) { serviceFactory = provider => provider.ResolveRequiredService <IProxyFactory>() .CreateProxy(descriptor.ServiceType, descriptor.ImplementType); } if (null != serviceFactory) { services.Add(new ServiceDefinition(descriptor.ServiceType, serviceFactory, descriptor.ServiceLifetime)); } else { services.Add(descriptor); } } } var container = services.Build(); DependencyResolver.SetDependencyResolver(container); return(container); }