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); }