目录
一:Autofac支持AOP
二:Autofac支持AOP的多种方式
一:类扩展
二:接口扩展
三:类扩展和接口扩展的区别
三:Autofac支持AOP扩展日志记录功能
四:Autofac支持AOP扩展缓存功能
五:Autofac支持AOP扩展IInterceptorSelector
当前项目右键管理Nuget包引入:Castle.core
扩展CusotmInterceptor类
using Castle.DynamicProxy;namespace Study_ASP.Net_Core_MVC.Services
{public class CusotmInterceptor : IInterceptor{/// /// 切入者逻辑/// /// public void Intercept(IInvocation invocation){//方法之前的逻辑Console.WriteLine("Begore");//执行真实的方法invocation.Proceed();//方法之后的逻辑Console.WriteLine("After");}}
}
修改关键类ApplePhone
using Autofac.Extras.DynamicProxy;
using Study_ASP.NET_Core_MVC.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.Net_Core_MVC.Services
{/// /// 标识特性支持AOP/// [Intercept(typeof(CusotmInterceptor))]public class ApplePhone : IPhone{public IMicroPhone MicroPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public IHeadPhone HeadPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public ApplePhone(){Console.WriteLine("{0}无参构造函数", this.GetType().Name);}public ApplePhone(IHeadPhone headPhone){Console.WriteLine("{0}有参构造函数:{1}", this.GetType().Name,headPhone);}/// /// 虚方法/// 打电话方法/// public virtual void Call(){Console.WriteLine("{0}打电话", this.GetType().Name);}/// /// 发短信方法/// public void Text(){Console.WriteLine("{0}发短信", this.GetType().Name);}/// /// 显示当前时间信息/// /// 时间信息/// public string ShowTime(string timeInfo){Console.WriteLine("显示时间:{0}", timeInfo);return $"当前时间:{timeInfo}";}}
}
修改Program.cs文件
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Autofac.Extras.DynamicProxy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;//表示整个应用程序,调用CreateBuilder方法创建一个WebApplicationBuilder对象。
var builder = WebApplication.CreateBuilder(args);
//向管道容器添加注册中间件
//添加注册控制器视图中间件
builder.Services.AddControllersWithViews();
//添加注册Session中间件
builder.Services.AddSession();//添加注册Autofac中间件
//通过工厂替换整合Autofac
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer(containerBuilder =>
{//注册服务containerBuilder.RegisterType().As();containerBuilder.RegisterType().As();//支持AAOP扩展containerBuilder.RegisterType().As().EnableClassInterceptors();//注册切入逻辑containerBuilder.RegisterType();//注册控制器和抽象关系var controllerBaseType = typeof(ControllerBase);//支持属性注入到ControllerBase中containerBuilder.RegisterAssemblyTypes(typeof(Program).Assembly).Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType).PropertiesAutowired(new CusotmPropertySelector());
});
//添加注册控制器属性注入
builder.Services.Replace(ServiceDescriptor.Transient());//配置管道容器中间件,构造WebApplication实例
var app = builder.Build();
//判断是否是开发模式
if (!app.Environment.IsDevelopment())
{//向管道中添加中间件,该中间件将捕获异常、记录异常、重置请求路径并重新执行请求。app.UseExceptionHandler("/Shared/Error");//向管道中添加用于使用HSTS的中间件,该中间件添加了Strict Transport Security标头。默认值为30天app.UseHsts();
}
//向管道添加Session中间件
app.UseSession();
//向管道添加用于将HTTP请求重定向到HTTPS的中间件。
app.UseHttpsRedirection();
//向管道添加为当前请求路径启用静态文件服务
app.UseStaticFiles();
//向管道添加路由配置中间件
app.UseRouting();
//向管道添加鉴权中间件
app.UseAuthentication();
//向管道添加授权中间件
app.UseAuthorization();
//向管道添加默认路由中间件
app.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
//向管道添加启动应用程序中间件
app.Run();
修改控制器文件
using Autofac;
using Microsoft.AspNetCore.Mvc;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;
using System.Diagnostics;namespace Study_ASP.NET_Core_MVC.Controllers
{public class HomeController : Controller{/// /// 定义构造函数/// private readonly ILogger _logger;private IPhone iPhone;/// /// 初始化构造函数/// /// 控制器构造函数/// 服务构造函数/// 上下文构造函数/// 自定义构造函数public HomeController(ILogger logger, IServiceProvider serviceProvider, IComponentContext componentContext, IPhone iPhone){//通过调试查看获取的数据_logger = logger;this.iPhone = iPhone;}/// /// Get请求/// Home控制器/// Index方法/// /// [HttpGet]public IActionResult Index(){//调用发短信方法iPhone.Text();//调用打电话方法iPhone.Call();//电泳显示实践方法ViewBag.ShowTime = iPhone.ShowTime(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"));return View();}}
}
扩展CusotmInterceptor类
using Castle.DynamicProxy;namespace Study_ASP.Net_Core_MVC.Services
{public class CusotmInterceptor : IInterceptor{/// /// 切入者逻辑/// /// public void Intercept(IInvocation invocation){//方法之前的逻辑Console.WriteLine("Begore");//执行真实的方法invocation.Proceed();//方法之后的逻辑Console.WriteLine("After");}}
}
修改关键类ApplePhone
using Autofac.Extras.DynamicProxy;
using Study_ASP.NET_Core_MVC.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.Net_Core_MVC.Services
{/// /// 标识特性支持AOP/// [Intercept(typeof(CusotmInterceptor))]public class ApplePhone : IPhone{public IMicroPhone MicroPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public IHeadPhone HeadPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public ApplePhone(){Console.WriteLine("{0}无参构造函数", this.GetType().Name);}public ApplePhone(IHeadPhone headPhone){Console.WriteLine("{0}有参构造函数:{1}", this.GetType().Name,headPhone);}/// /// 虚方法/// 打电话方法/// public virtual void Call(){Console.WriteLine("{0}打电话", this.GetType().Name);}/// /// 发短信方法/// public void Text(){Console.WriteLine("{0}发短信", this.GetType().Name);}/// /// 显示当前时间信息/// /// 时间信息/// public string ShowTime(string timeInfo){Console.WriteLine("显示时间:{0}", timeInfo);return $"当前时间:{timeInfo}";}}
}
修改Program.cs文件
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Autofac.Extras.DynamicProxy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;//表示整个应用程序,调用CreateBuilder方法创建一个WebApplicationBuilder对象。
var builder = WebApplication.CreateBuilder(args);
//向管道容器添加注册中间件
//添加注册控制器视图中间件
builder.Services.AddControllersWithViews();
//添加注册Session中间件
builder.Services.AddSession();//添加注册Autofac中间件
//通过工厂替换整合Autofac
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer(containerBuilder =>
{//注册服务containerBuilder.RegisterType().As();containerBuilder.RegisterType().As();//支持AAOP扩展--类扩展containerBuilder.RegisterType().As().EnableClassInterceptors();//注册切入逻辑containerBuilder.RegisterType();//注册控制器和抽象关系var controllerBaseType = typeof(ControllerBase);//支持属性注入到ControllerBase中containerBuilder.RegisterAssemblyTypes(typeof(Program).Assembly).Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType).PropertiesAutowired(new CusotmPropertySelector());
});
//添加注册控制器属性注入
builder.Services.Replace(ServiceDescriptor.Transient());//配置管道容器中间件,构造WebApplication实例
var app = builder.Build();
//判断是否是开发模式
if (!app.Environment.IsDevelopment())
{//向管道中添加中间件,该中间件将捕获异常、记录异常、重置请求路径并重新执行请求。app.UseExceptionHandler("/Shared/Error");//向管道中添加用于使用HSTS的中间件,该中间件添加了Strict Transport Security标头。默认值为30天app.UseHsts();
}
//向管道添加Session中间件
app.UseSession();
//向管道添加用于将HTTP请求重定向到HTTPS的中间件。
app.UseHttpsRedirection();
//向管道添加为当前请求路径启用静态文件服务
app.UseStaticFiles();
//向管道添加路由配置中间件
app.UseRouting();
//向管道添加鉴权中间件
app.UseAuthentication();
//向管道添加授权中间件
app.UseAuthorization();
//向管道添加默认路由中间件
app.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
//向管道添加启动应用程序中间件
app.Run();
修改控制器文件
using Autofac;
using Microsoft.AspNetCore.Mvc;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;
using System.Diagnostics;namespace Study_ASP.NET_Core_MVC.Controllers
{public class HomeController : Controller{/// /// 定义构造函数/// private readonly ILogger _logger;private IPhone iPhone;/// /// 初始化构造函数/// /// 控制器构造函数/// 服务构造函数/// 上下文构造函数/// 自定义构造函数public HomeController(ILogger logger, IServiceProvider serviceProvider, IComponentContext componentContext, IPhone iPhone){//通过调试查看获取的数据_logger = logger;this.iPhone = iPhone;}/// /// Get请求/// Home控制器/// Index方法/// /// [HttpGet]public IActionResult Index(){//调用发短信方法iPhone.Text();//调用打电话方法iPhone.Call();//显示时间方法ViewBag.ShowTime = iPhone.ShowTime(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"));return View();}}
}
扩展CusotmInterceptor类
using Castle.DynamicProxy;namespace Study_ASP.Net_Core_MVC.Services
{public class CusotmInterceptor : IInterceptor{/// /// 切入者逻辑/// /// public void Intercept(IInvocation invocation){//方法之前的逻辑Console.WriteLine("Begore");//执行真实的方法invocation.Proceed();//方法之后的逻辑Console.WriteLine("After");}}
}
修改关键类IPhone
using Autofac.Extras.DynamicProxy;
using DocumentFormat.OpenXml.Drawing.Charts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.NET_Core_MVC.Interfaces
{/// /// 标识特性支持AOP/// [Intercept(typeof(CusotmInterceptor))]public interface IPhone{IMicroPhone MicroPhone { get; set; }IHeadPhone HeadPhone { get; set; }/// /// 打电话方法/// void Call();/// /// 发短信方法/// void Text();/// /// 显示当前时间信息/// /// 时间信息/// string ShowTime(string timeInfo);}public interface IMicroPhone { }public interface IHeadPhone { }
}
修改Program.cs文件
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Autofac.Extras.DynamicProxy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;//表示整个应用程序,调用CreateBuilder方法创建一个WebApplicationBuilder对象。
var builder = WebApplication.CreateBuilder(args);
//向管道容器添加注册中间件
//添加注册控制器视图中间件
builder.Services.AddControllersWithViews();
//添加注册Session中间件
builder.Services.AddSession();//添加注册Autofac中间件
//通过工厂替换整合Autofac
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer(containerBuilder =>
{//注册服务containerBuilder.RegisterType().As();containerBuilder.RegisterType().As();//支持AAOP扩展--接口扩展containerBuilder.RegisterType().As().EnableInterfaceInterceptors();//注册切入逻辑containerBuilder.RegisterType();//注册控制器和抽象关系var controllerBaseType = typeof(ControllerBase);//支持属性注入到ControllerBase中containerBuilder.RegisterAssemblyTypes(typeof(Program).Assembly).Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType).PropertiesAutowired(new CusotmPropertySelector());
});
//添加注册控制器属性注入
builder.Services.Replace(ServiceDescriptor.Transient());//配置管道容器中间件,构造WebApplication实例
var app = builder.Build();
//判断是否是开发模式
if (!app.Environment.IsDevelopment())
{//向管道中添加中间件,该中间件将捕获异常、记录异常、重置请求路径并重新执行请求。app.UseExceptionHandler("/Shared/Error");//向管道中添加用于使用HSTS的中间件,该中间件添加了Strict Transport Security标头。默认值为30天app.UseHsts();
}
//向管道添加Session中间件
app.UseSession();
//向管道添加用于将HTTP请求重定向到HTTPS的中间件。
app.UseHttpsRedirection();
//向管道添加为当前请求路径启用静态文件服务
app.UseStaticFiles();
//向管道添加路由配置中间件
app.UseRouting();
//向管道添加鉴权中间件
app.UseAuthentication();
//向管道添加授权中间件
app.UseAuthorization();
//向管道添加默认路由中间件
app.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
//向管道添加启动应用程序中间件
app.Run();
修改控制器文件
using Autofac;
using Microsoft.AspNetCore.Mvc;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;
using System.Diagnostics;namespace Study_ASP.NET_Core_MVC.Controllers
{public class HomeController : Controller{/// /// 定义构造函数/// private readonly ILogger _logger;private IPhone iPhone;/// /// 初始化构造函数/// /// 控制器构造函数/// 服务构造函数/// 上下文构造函数/// 自定义构造函数public HomeController(ILogger logger, IServiceProvider serviceProvider, IComponentContext componentContext, IPhone iPhone){//通过调试查看获取的数据_logger = logger;this.iPhone = iPhone;}/// /// Get请求/// Home控制器/// Index方法/// /// [HttpGet]public IActionResult Index(){//调用发短信方法iPhone.Text();//调用打电话方法iPhone.Call();//电泳显示实践方法ViewBag.ShowTime = iPhone.ShowTime(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"));return View();}}
}
通过EnableClassInterceptors类扩展时:
- 需要将Intercept标记到具体类上
- 只有在这个具体类上,标记了虚方法,虚方法才可以进入到IInterceptor中,来支持AOP扩展。
通过EnableInterfaceInterceptors接口扩展时:
- 需要将Intercept标记到抽象类上
- 只要是实现了这个接口,无论是否是虚方法,都可以进入到IInterceptor中,来支持AOP扩展。
当前项目右键管理Nuget包引入:Microsoft.Extensions.Logging.Abstractions、Newtonsoft.Json
扩展CusotmLogInterceptor类
using Castle.Core.Logging;
using Castle.DynamicProxy;
using System;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.NET_Core_MVC.Interfaces
{public class CusotmLogInterceptor : IInterceptor{/// /// 定义构造函数/// private readonly ILogger _ILogger;/// /// 初始化构造函数/// /// public CusotmLogInterceptor(ILogger logger){this._ILogger = logger;}/// /// 切入者逻辑/// /// public void Intercept(IInvocation invocation){//方法之前的逻辑Console.WriteLine("Begore");_ILogger.LogInformation($"参数:{Newtonsoft.Json.JsonConvert.SerializeObject(invocation.Arguments)}");_ILogger.LogInformation($"方法{invocation.Method.Name} 开始执行");//执行真实的方法invocation.Proceed();_ILogger.LogInformation($"方法{invocation.Method.Name} 执行完毕");//方法之后的逻辑Console.WriteLine("After");_ILogger.LogInformation($"结果:{Newtonsoft.Json.JsonConvert.SerializeObject(invocation.ReturnValue)}");}}
}
修改关键类IPhone
using Autofac.Extras.DynamicProxy;
using DocumentFormat.OpenXml.Drawing.Charts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.NET_Core_MVC.Interfaces
{/// /// 标识特性支持AOP/// [Intercept(typeof(CusotmInterceptor))][Intercept(typeof(CusotmLogInterceptor))]public interface IPhone{IMicroPhone MicroPhone { get; set; }IHeadPhone HeadPhone { get; set; }/// /// 打电话方法/// void Call();/// /// 发短信方法/// void Text();/// /// 玩游戏方法/// /// /// object PlayGame(object play);/// /// 显示当前时间信息/// /// 时间信息/// string ShowTime(string timeInfo);}public interface IMicroPhone { }public interface IHeadPhone { }
}
修改关键类ApplePhone
using Autofac.Extras.DynamicProxy;
using Study_ASP.NET_Core_MVC.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.Net_Core_MVC.Services
{public class ApplePhone : IPhone{public IMicroPhone MicroPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public IHeadPhone HeadPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public ApplePhone(){Console.WriteLine("{0}无参构造函数", this.GetType().Name);}public ApplePhone(IHeadPhone headPhone){Console.WriteLine("{0}有参构造函数:{1}", this.GetType().Name,headPhone);}/// /// 虚方法/// 打电话方法/// public virtual void Call(){Console.WriteLine("{0}打电话", this.GetType().Name);}/// /// 发短信方法/// public void Text(){Console.WriteLine("{0}发短信", this.GetType().Name);}/// /// 玩游戏方法/// /// /// public object PlayGame(object play){return new{Id = 123,Name = "VinCente"};}/// /// 显示当前时间信息/// /// 时间信息/// public string ShowTime(string timeInfo){Console.WriteLine("显示时间:{0}", timeInfo);return $"当前时间:{timeInfo}";}}
}
修改Program.cs文件
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Autofac.Extras.DynamicProxy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;//表示整个应用程序,调用CreateBuilder方法创建一个WebApplicationBuilder对象。
var builder = WebApplication.CreateBuilder(args);
//向管道容器添加注册中间件
//添加注册控制器视图中间件
builder.Services.AddControllersWithViews();
//添加注册Session中间件
builder.Services.AddSession();//添加注册Autofac中间件
//通过工厂替换整合Autofac
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer(containerBuilder =>
{//注册服务containerBuilder.RegisterType().As();containerBuilder.RegisterType().As();//支持AAOP扩展--接口扩展containerBuilder.RegisterType().As().EnableInterfaceInterceptors();//注册切入逻辑containerBuilder.RegisterType();containerBuilder.RegisterType();//注册控制器和抽象关系var controllerBaseType = typeof(ControllerBase);//支持属性注入到ControllerBase中containerBuilder.RegisterAssemblyTypes(typeof(Program).Assembly).Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType).PropertiesAutowired(new CusotmPropertySelector());
});
//添加注册控制器属性注入
builder.Services.Replace(ServiceDescriptor.Transient());//配置管道容器中间件,构造WebApplication实例
var app = builder.Build();
//判断是否是开发模式
if (!app.Environment.IsDevelopment())
{//向管道中添加中间件,该中间件将捕获异常、记录异常、重置请求路径并重新执行请求。app.UseExceptionHandler("/Shared/Error");//向管道中添加用于使用HSTS的中间件,该中间件添加了Strict Transport Security标头。默认值为30天app.UseHsts();
}
//向管道添加Session中间件
app.UseSession();
//向管道添加用于将HTTP请求重定向到HTTPS的中间件。
app.UseHttpsRedirection();
//向管道添加为当前请求路径启用静态文件服务
app.UseStaticFiles();
//向管道添加路由配置中间件
app.UseRouting();
//向管道添加鉴权中间件
app.UseAuthentication();
//向管道添加授权中间件
app.UseAuthorization();
//向管道添加默认路由中间件
app.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
//向管道添加启动应用程序中间件
app.Run();
修改控制器文件
using Autofac;
using Microsoft.AspNetCore.Mvc;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;
using System.Diagnostics;namespace Study_ASP.NET_Core_MVC.Controllers
{public class HomeController : Controller{/// /// 定义构造函数/// private readonly ILogger _logger;private IPhone iPhone;/// /// 初始化构造函数/// /// 控制器构造函数/// 服务构造函数/// 上下文构造函数/// 自定义构造函数public HomeController(ILogger logger, IServiceProvider serviceProvider, IComponentContext componentContext, IPhone iPhone){//通过调试查看获取的数据_logger = logger;this.iPhone = iPhone;}/// /// Get请求/// Home控制器/// Index方法/// /// [HttpGet]public IActionResult Index(){//调用发短信方法iPhone.Text();//调用打电话方法iPhone.Call();//调用玩游戏方法object oInstance = iPhone.PlayGame(new { playId = 123, playName = "我要玩游戏" });//电泳显示实践方法ViewBag.ShowTime = iPhone.ShowTime(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"));return View();}}
}
结果截图
扩展CusotmCacheInterceptor类:
using Castle.DynamicProxy;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.NET_Core_MVC.Interfaces
{public class CusotmCacheInterceptor : IInterceptor{/// /// 定义构造函数/// private readonly ILogger _ILogger;/// /// 初始化构造函数/// /// public CusotmCacheInterceptor(ILogger logger){this._ILogger = logger;}//定义字典private static Dictionary _cacheDictionary = new Dictionary();/// /// 切入者逻辑/// /// public void Intercept(IInvocation invocation){//方法之前检查缓存的结果//定义Keystring cacheKey = invocation.Method.Name;//判断当前是否有缓存结果if (_cacheDictionary.ContainsKey(cacheKey)){invocation.ReturnValue = _cacheDictionary[cacheKey];}else{//执行真实的方法invocation.Proceed();//方法之后保存缓存的结果_cacheDictionary[cacheKey] = invocation.ReturnValue;}}}
}
修改关键类IPhone
using Autofac.Extras.DynamicProxy;
using DocumentFormat.OpenXml.Drawing.Charts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.NET_Core_MVC.Interfaces
{/// /// 标识特性支持AOP/// [Intercept(typeof(CusotmCacheInterceptor))]public interface IPhone{IMicroPhone MicroPhone { get; set; }IHeadPhone HeadPhone { get; set; }/// /// 打电话方法/// void Call();/// /// 发短信方法/// void Text();/// /// 玩游戏方法/// /// /// object PlayGame(object play);/// /// 显示当前时间信息/// /// 时间信息/// string ShowTime(string timeInfo);}public interface IMicroPhone { }public interface IHeadPhone { }
}
修改关键类ApplePhone
using Autofac.Extras.DynamicProxy;
using Study_ASP.NET_Core_MVC.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.Net_Core_MVC.Services
{public class ApplePhone : IPhone{public IMicroPhone MicroPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public IHeadPhone HeadPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public ApplePhone(){Console.WriteLine("{0}无参构造函数", this.GetType().Name);}public ApplePhone(IHeadPhone headPhone){Console.WriteLine("{0}有参构造函数:{1}", this.GetType().Name,headPhone);}/// /// 虚方法/// 打电话方法/// public virtual void Call(){Console.WriteLine("{0}打电话", this.GetType().Name);}/// /// 发短信方法/// public void Text(){Console.WriteLine("{0}发短信", this.GetType().Name);}/// /// 玩游戏方法/// /// /// public object PlayGame(object play){return new{Id = 123,Name = "VinCente",DateTime = DateTime.Now};}/// /// 显示当前时间信息/// /// 时间信息/// public string ShowTime(string timeInfo){Console.WriteLine("显示时间:{0}", timeInfo);return $"当前时间:{timeInfo}";}}
}
修改Program.cs文件
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Autofac.Extras.DynamicProxy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;//表示整个应用程序,调用CreateBuilder方法创建一个WebApplicationBuilder对象。
var builder = WebApplication.CreateBuilder(args);
//向管道容器添加注册中间件
//添加注册控制器视图中间件
builder.Services.AddControllersWithViews();
//添加注册Session中间件
builder.Services.AddSession();//添加注册Autofac中间件
//通过工厂替换整合Autofac
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer(containerBuilder =>
{//注册服务containerBuilder.RegisterType().As();containerBuilder.RegisterType().As();//支持AAOP扩展--接口扩展containerBuilder.RegisterType().As().EnableInterfaceInterceptors();//注册切入逻辑containerBuilder.RegisterType();containerBuilder.RegisterType();containerBuilder.RegisterType();//注册控制器和抽象关系var controllerBaseType = typeof(ControllerBase);//支持属性注入到ControllerBase中containerBuilder.RegisterAssemblyTypes(typeof(Program).Assembly).Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType).PropertiesAutowired(new CusotmPropertySelector());
});
//添加注册控制器属性注入
builder.Services.Replace(ServiceDescriptor.Transient());//配置管道容器中间件,构造WebApplication实例
var app = builder.Build();
//判断是否是开发模式
if (!app.Environment.IsDevelopment())
{//向管道中添加中间件,该中间件将捕获异常、记录异常、重置请求路径并重新执行请求。app.UseExceptionHandler("/Shared/Error");//向管道中添加用于使用HSTS的中间件,该中间件添加了Strict Transport Security标头。默认值为30天app.UseHsts();
}
//向管道添加Session中间件
app.UseSession();
//向管道添加用于将HTTP请求重定向到HTTPS的中间件。
app.UseHttpsRedirection();
//向管道添加为当前请求路径启用静态文件服务
app.UseStaticFiles();
//向管道添加路由配置中间件
app.UseRouting();
//向管道添加鉴权中间件
app.UseAuthentication();
//向管道添加授权中间件
app.UseAuthorization();
//向管道添加默认路由中间件
app.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
//向管道添加启动应用程序中间件
app.Run();
修改控制器文件
using Autofac;
using Microsoft.AspNetCore.Mvc;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;
using System.Diagnostics;namespace Study_ASP.NET_Core_MVC.Controllers
{public class HomeController : Controller{/// /// 定义构造函数/// private readonly ILogger _logger;private IPhone iPhone;/// /// 初始化构造函数/// /// 控制器构造函数/// 服务构造函数/// 上下文构造函数/// 自定义构造函数public HomeController(ILogger logger, IServiceProvider serviceProvider, IComponentContext componentContext, IPhone iPhone){//通过调试查看获取的数据_logger = logger;this.iPhone = iPhone;}/// /// Get请求/// Home控制器/// Index方法/// /// [HttpGet]public IActionResult Index(){//调用发短信方法iPhone.Text();//调用打电话方法iPhone.Call();//调用玩游戏方法object oInstance = iPhone.PlayGame(new { playId = 123, playName = "我要玩游戏" });//显示时间方法ViewBag.ShowTime = iPhone.ShowTime(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"));return View();}}
}
结果截图
以上扩展方法需要在方法上标注特性。扩展使用IInterceptorSelector时,只需要在Program中注册服务扩展接口时注册,无需在方法上标注特性。
扩展CusotmInterceptorSelector类
using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.NET_Core_MVC.Interfaces
{public class CusotmInterceptorSelector : IInterceptorSelector{/// /// 让我们选择使用那个IInterceptor/// /// /// /// /// /// public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors){return new IInterceptor[]{new CusotmInterceptor(),new CusotmCacheInterceptor()};}}
}
修改关键类Program.cs文件
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Autofac.Extras.DynamicProxy;
using Castle.DynamicProxy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;//表示整个应用程序,调用CreateBuilder方法创建一个WebApplicationBuilder对象。
var builder = WebApplication.CreateBuilder(args);
//向管道容器添加注册中间件
//添加注册控制器视图中间件
builder.Services.AddControllersWithViews();
//添加注册Session中间件
builder.Services.AddSession();//添加注册Autofac中间件
//通过工厂替换整合Autofac
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer(containerBuilder =>
{//注册服务containerBuilder.RegisterType().As();containerBuilder.RegisterType().As();//支持AAOP扩展--接口扩展containerBuilder.RegisterType().As().EnableInterfaceInterceptors(new ProxyGenerationOptions(){Selector= new CusotmInterceptorSelector()});//注册切入逻辑containerBuilder.RegisterType();containerBuilder.RegisterType();containerBuilder.RegisterType();//注册控制器和抽象关系var controllerBaseType = typeof(ControllerBase);//支持属性注入到ControllerBase中containerBuilder.RegisterAssemblyTypes(typeof(Program).Assembly).Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType).PropertiesAutowired(new CusotmPropertySelector());
});
//添加注册控制器属性注入
builder.Services.Replace(ServiceDescriptor.Transient());//配置管道容器中间件,构造WebApplication实例
var app = builder.Build();
//判断是否是开发模式
if (!app.Environment.IsDevelopment())
{//向管道中添加中间件,该中间件将捕获异常、记录异常、重置请求路径并重新执行请求。app.UseExceptionHandler("/Shared/Error");//向管道中添加用于使用HSTS的中间件,该中间件添加了Strict Transport Security标头。默认值为30天app.UseHsts();
}
//向管道添加Session中间件
app.UseSession();
//向管道添加用于将HTTP请求重定向到HTTPS的中间件。
app.UseHttpsRedirection();
//向管道添加为当前请求路径启用静态文件服务
app.UseStaticFiles();
//向管道添加路由配置中间件
app.UseRouting();
//向管道添加鉴权中间件
app.UseAuthentication();
//向管道添加授权中间件
app.UseAuthorization();
//向管道添加默认路由中间件
app.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
//向管道添加启动应用程序中间件
app.Run();
修改关键类IPhone
using Autofac.Extras.DynamicProxy;
using DocumentFormat.OpenXml.Drawing.Charts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.NET_Core_MVC.Interfaces
{public interface IPhone{IMicroPhone MicroPhone { get; set; }IHeadPhone HeadPhone { get; set; }/// /// 打电话方法/// void Call();/// /// 发短信方法/// void Text();/// /// 玩游戏方法/// /// /// object PlayGame(object play);/// /// 显示当前时间信息/// /// 时间信息/// string ShowTime(string timeInfo);}public interface IMicroPhone { }public interface IHeadPhone { }
}
修改关键类ApplePhone
using Autofac.Extras.DynamicProxy;
using Study_ASP.NET_Core_MVC.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.Net_Core_MVC.Services
{public class ApplePhone : IPhone{public IMicroPhone MicroPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public IHeadPhone HeadPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public ApplePhone(){Console.WriteLine("{0}无参构造函数", this.GetType().Name);}public ApplePhone(IHeadPhone headPhone){Console.WriteLine("{0}有参构造函数:{1}", this.GetType().Name,headPhone);}/// /// 虚方法/// 打电话方法/// public virtual void Call(){Console.WriteLine("{0}打电话", this.GetType().Name);}/// /// 发短信方法/// public void Text(){Console.WriteLine("{0}发短信", this.GetType().Name);}/// /// 玩游戏方法/// /// /// public object PlayGame(object play){return new{Id = 123,Name = "VinCente",DateTime = DateTime.Now};}/// /// 显示当前时间信息/// /// 时间信息/// public string ShowTime(string timeInfo){Console.WriteLine("显示时间:{0}", timeInfo);return $"当前时间:{timeInfo}";}}
}
结果截图
上一篇: 数学教师教育随笔