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 @ 2022-06-19 16:15  ziff123  阅读(133)  评论(0编辑  收藏  举报