class Program
{
static void Main(string[] args)
{
//创建一个容器
ContainerBuilder builder = new ContainerBuilder();
//注册UserService
builder.RegisterType<UserService>().As<IUserService>()
.EnableInterfaceInterceptors();//通过接口方式完成aop扩展
builder.RegisterType<CustomInterceptor>();//通过接口方式完成aop扩展
//从容器中解析出UserService
IContainer container = builder.Build();
IUserService a = container.Resolve<IUserService>();
//执行UserService的方法
a.show();
}
}
//生产一个 UserService类
public class UserService : IUserService
{
public void show()
{
Console.WriteLine("UserService 执行");
}
}
[Intercept(typeof(CustomInterceptor))]//通过接口方式完成aop扩展
public interface IUserService
{
void show();
}
using Castle.DynamicProxy; namespace autofac_aop测试; public class CustomInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { Console.WriteLine("================================================"); Console.WriteLine("=================在XX业务逻辑前执行=============="); Console.WriteLine("================================================="); invocation.Proceed(); Console.WriteLine("==================================================="); Console.WriteLine("=================在XX业务逻辑后执行================"); Console.WriteLine("==================================================="); } }标签:autofac,Console,void,扩展,WriteLine,aop,UserService,public From: https://www.cnblogs.com/hlm750908/p/18598378