Autofac 支持AOP

1、Nuget引入Castle.Core

2、新建CustomInterceptor类,实现IInterceptor接口

复制代码
 1 using Castle.DynamicProxy;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace Business.FrameWork.AutofacExt.AOP
 9 {
10     public class CustomInterceptor : IInterceptor
11     {
12         public void Intercept(IInvocation invocation)
13         {
14             Console.WriteLine("Before");
15             invocation.Proceed();
16             Console.WriteLine("After");
17         }
18     }
19 }
复制代码

3、在Program.cs注册CustomInterceptor

复制代码
 1 //第一种方式注入
 2     builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());  //通过工厂替换,把Autofac整合进来
 3     builder.Host.ConfigureContainer<ContainerBuilder>(option =>
 4     {
 5         option.RegisterType<Microphone>().As<IMicrophone>().EnableClassInterceptors();  //支持AOP
 6         option.RegisterType<Power>().As<IPower>();
 7         option.RegisterType<Headphone>().As<Headphone>();
 8         option.RegisterType<Headphone>().As<IHeadphone>();
 9         option.RegisterType<CustomInterceptor>();
10     });
复制代码

4、IMicrophone接口

1 namespace Business.IServices
2 {
3     public interface IMicrophone
4     {
5         void Call();
6         void Text();
7     }
8 }

5、Microphone实现IMicrophone

复制代码
 1 using Autofac.Extras.DynamicProxy;
 2 using Business.FrameWork.AutofacExt.AOP;
 3 using Business.IServices;
 4 
 5 namespace Business.Services
 6 {
 7     [Intercept(typeof(CustomInterceptor))]
 8     public class Microphone : IMicrophone
 9     {
10         public Microphone()
11         {
12             Console.WriteLine($"{this.GetType().Name}被构造..");
13         }
14 
15         public void Text()
16         {
17             Console.WriteLine("text.....");
18         }
19 
20         public virtual void Call()
21         {
22             Console.WriteLine("Call.....");
23         }
24     }
25 }
复制代码

6、在控制器调用

复制代码
 1 using Autofac;
 2 using Business.IServices;
 3 using Microsoft.AspNetCore.Mvc;
 4 
 5 namespace ProjectIOC.Controllers
 6 {
 7     public class HomeController : Controller
 8     {
 9         private readonly IMicrophone _microphone;
10         private readonly IMicrophone _microphone2;
11         private readonly IMicrophone _microphone3;
12 
13         public HomeController(IMicrophone microphone,IServiceProvider serviceProvider,IComponentContext componentContext)
14         {
15             this._microphone = microphone;
16             this._microphone2 = serviceProvider.GetService<IMicrophone>();
17             this._microphone3 = componentContext.Resolve<IMicrophone>();
18         }
19 
20         public IActionResult Index()
21         {
22             _microphone.Text();
23             _microphone.Call();
24             return View();
25         }
26     }
27 }
复制代码

7、调试发现,执行virtual方法Call,会先进入Intercept

 8、第二种方式

复制代码
 1 //第一种方式注入
 2     builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());  //通过工厂替换,把Autofac整合进来
 3     builder.Host.ConfigureContainer<ContainerBuilder>(option =>
 4     {
 5         //option.RegisterType<Microphone>().As<IMicrophone>().EnableClassInterceptors();  //支持AOP
 6         option.RegisterType<Microphone>().As<IMicrophone>().EnableInterfaceInterceptors(); //支持AOP
 7         option.RegisterType<Power>().As<IPower>();
 8         option.RegisterType<Headphone>().As<Headphone>();
 9         option.RegisterType<Headphone>().As<IHeadphone>();
10         option.RegisterType<CustomInterceptor>();
11     });
复制代码

9、将标记写在接口上

复制代码
 1 using Autofac.Extras.DynamicProxy;
 2 using Business.FrameWork.AutofacExt.AOP;
 3 
 4 namespace Business.IServices
 5 {
 6     [Intercept(typeof(CustomInterceptor))]
 7     public interface IMicrophone
 8     {
 9         void Call();
10         void Text();
11     }
12 }
复制代码

 

10、重新运行,可以发现,Text()和Call()方法,调用前都会先进入Intercept

 

posted @   ziff123  阅读(134)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示