public class CustomAutofacAop : IInterceptor { public void Intercept(IInvocation invocation) { { Console.WriteLine("方法执行前"); } invocation.Proceed();//执行这句话就是去执行具体的实例的这个方法 { Console.WriteLine("方法执行后"); } } }
[Intercept(typeof(CustomAutofacAop))] //AOP能够在当前接口生效 public interface ICustomServiceA { void Show(); }
在startup里 containerBuilder.RegisterType(typeof(CustomAutofacAop));
containerBuilder.RegisterType<CustomServiceA>().As<ICustomServiceA>().EnableInterfaceInterceptors(); //接口
原文:https://www.cnblogs.com/wangmaolin/p/14491820.html