/// <summary> /// 使用 CZGL.AOP 处理容器中需要代理的类型 /// </summary> /// <param name="service"></param> /// <returns></returns> public static IContainer BuildAopProxy(this IContainer service) { if (service is null) { throw new ArgumentNullException(nameof(service)); } ContainerBuilder proxyServiceCollection = new ContainerBuilder(); IEnumerable <IComponentRegistration> serviceList = service.ComponentRegistry.Registrations; foreach (ComponentRegistration item in serviceList) { Type newType; Type serviceType = item.Services.OfType <IServiceWithType>().Select(x => x.ServiceType).FirstOrDefault(); Type implementationType = item.Activator.LimitType; if (implementationType?.GetCustomAttribute(typeof(InterceptorAttribute)) == null) { proxyServiceCollection.RegisterComponent(item); continue; } if (serviceType == implementationType) { newType = DynamicProxy.CreateProxyClassType(implementationType); } else { newType = DynamicProxy.CreateProxyClassType(serviceType, implementationType, false); } proxyServiceCollection.RegisterComponent(item); proxyServiceCollection.RegisterType(newType).As(serviceType); } return(proxyServiceCollection.Build()); }
/// <summary> /// 使用 CZGL.AOP 处理容器中需要代理的类型 /// </summary> /// <param name="service"></param> /// <returns></returns> public static IServiceCollection BuildAopProxy(this IServiceCollection service) { if (service is null) { throw new ArgumentNullException(nameof(service)); } IServiceCollection proxyServiceCollection = new ServiceCollection(); // 将容器中的设置拦截器的类型生成新的类型代理 foreach (ServiceDescriptor item in service) { // 第一步,判断是否为继承 // 第二步,传递暴露的接口或其它服务 // 第三步,传递参数并生成代理类型 // 第四步,判断作用域生命周期 // 第五步,添加到容器中 Type newType; Type serviceType = item.ServiceType; Type implementationType = item.ImplementationType; if (implementationType is null) { if (item.ImplementationInstance != null) { switch (item.Lifetime) { case ServiceLifetime.Singleton: proxyServiceCollection.AddSingleton(serviceType, item.ImplementationInstance); break; case ServiceLifetime.Scoped: proxyServiceCollection.AddScoped(serviceType); break; case ServiceLifetime.Transient: proxyServiceCollection.AddSingleton(serviceType, item.ImplementationInstance); break; } } else { switch (item.Lifetime) { case ServiceLifetime.Singleton: proxyServiceCollection.AddSingleton(serviceType, item.ImplementationFactory); break; case ServiceLifetime.Scoped: proxyServiceCollection.AddScoped(serviceType, item.ImplementationFactory); break; case ServiceLifetime.Transient: proxyServiceCollection.AddTransient(serviceType, item.ImplementationFactory); break; } } continue; } if (serviceType == implementationType) { newType = DynamicProxy.CreateProxyClassType(item.ImplementationType); } else { newType = DynamicProxy.CreateProxyClassType(item.ServiceType, item.ImplementationType, false); } proxyServiceCollection.Add(ServiceDescriptor.Describe(serviceType, newType, item.Lifetime)); } return(proxyServiceCollection); }
/// <summary> /// 使用 CZGL.AOP 处理容器中需要代理的类型 /// </summary> /// <param name="service"></param> /// <returns></returns> private static IServiceCollection CreateBuildAopProxy(this IServiceCollection service) { IServiceCollection proxyServiceCollection = new ServiceCollection(); foreach (ServiceDescriptor item in service) { Type newType; Type serviceType = item.ServiceType; Type implementationType = item.ImplementationType; if (implementationType is null) { if (item.ImplementationInstance != null) { switch (item.Lifetime) { case ServiceLifetime.Singleton: proxyServiceCollection.AddSingleton(serviceType, item.ImplementationInstance); break; case ServiceLifetime.Scoped: proxyServiceCollection.AddScoped(serviceType); break; case ServiceLifetime.Transient: proxyServiceCollection.AddSingleton(serviceType, item.ImplementationInstance); break; } } else { switch (item.Lifetime) { case ServiceLifetime.Singleton: proxyServiceCollection.AddSingleton(serviceType, item.ImplementationFactory); break; case ServiceLifetime.Scoped: proxyServiceCollection.AddScoped(serviceType, item.ImplementationFactory); break; case ServiceLifetime.Transient: proxyServiceCollection.AddTransient(serviceType, item.ImplementationFactory); break; } } continue; } if (serviceType == implementationType) { newType = DynamicProxy.CreateProxyClassType(item.ImplementationType); } else { newType = DynamicProxy.CreateProxyClassType(item.ServiceType, item.ImplementationType, false); } proxyServiceCollection.Add(ServiceDescriptor.Describe(serviceType, newType, item.Lifetime)); } return(proxyServiceCollection); }