net 5 aop

  接口处 声明一个切入逻辑

 

 标记接口 所有的方法都要aop 了

 

   [Intercept(typeof(CustomAutofactAOP))]
    public interface Itest
    {
        public void hello();
     
    }

 

 public class CustomAutofactAOP : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            {
                Console.WriteLine("执行前。。。。");
            }
            invocation.Proceed();// 
            {
                Console.WriteLine("执行后。。。。");
            }
        }
    }

针对接口注册

 

 

       public static void  RegsterType(this ContainerBuilder builder)
        {

            builder.RegisterType<CustomAutofactAOP>();
            builder.RegisterType<test>().As<Itest>().EnableInterfaceInterceptors();
            builder.RegisterType<CustomAuthorizationHandler>().As<IAuthorizationHandler>();
            builder.RegisterType<World>().As<IWorld>().PropertiesAutowired(new CustomPropertySelector());
        }


调用hello 方法

 


 

运行结果 


 


 还有通过虚方法aop  这样细节到具体方法 而不是所有接口类都aop

 



           1 builder.RegisterType<test>().As<Itest>().EnableInterfaceInterceptors(); 换成 builder.RegisterType<test>().As<Itest>().Enablec1assInterceptors();
2 在具体实现的抽象类上标记拦截器   [Intercept(typeof(CustomAutofactAOP))] 放在整个实现类上 需要aop的方法 标记虚方法 virtual 即可完成aop


posted @ 2021-09-24 15:43  非著名架构师  阅读(141)  评论(0)    收藏  举报