这篇文章主要介绍了Asp.Net Core轻量级Aop解决方案:AspectCore,需要的朋友可以参考下
什么是AspectCore Project ?
AspectCore Project 是适用于Asp.Net Core 平台的轻量级 Aop(Aspect-oriented programming) 解决方案,它更好的遵循Asp.Net Core的模块化开发理念,使用AspectCore可以更容易构建低耦合、易扩展的Web应用程序。AspectCore使用Emit实现高效的动态代理从而不依赖任何第三方Aop库。
开使使用AspectCore
启动 Visual Studio。从 File 菜单, 选择 New > Project。选择 ASP.NET Core Web Application 项目模版,创建新的 ASP.NET Core Web Application 项目。
从 Nuget 安装 AspectCore.Extensions.DependencyInjection package:
PM> Install-Package AspectCore.Extensions.DependencyInjection
在一般情况下可以使用抽象的InterceptorAttribute自定义特性类,它实现IInterceptor接口。AspectCore默认实现了基于Attribute的拦截器配置。我们的自定义拦截器看起来像下面这样:
public class CustomInterceptorAttribute : InterceptorAttribute { public async override Task Invoke(IAspectContext context, AspectDelegate next) { try { Console.WriteLine("Before service call"); await next(context); } catch (Exception) { Console.WriteLine("Service threw an exception!"); throw; } finally { Console.WriteLine("After service call"); } } }
定义ICustomService接口和它的实现类CustomService:
public interface ICustomService { [CustomInterceptor] void Call(); } public class CustomService : ICustomService { public void Call() { Console.WriteLine("service calling..."); } }
在HomeController中注入ICustomService:
public class HomeController : Controller { private readonly ICustomService _service; public HomeController(ICustomService service) { _service = service; } public IActionResult Index() { _service.Call(); return View(); } }
注册ICustomService,接着,在ConfigureServices中配置创建代理类型的容器:
public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddTransient<ICustomService, CustomService>(); services.AddMvc(); services.AddAspectCore(); return services.BuildAspectCoreServiceProvider(); }
拦截器配置。首先安装AspectCore.Extensions.Configuration package:
PM> Install-Package AspectCore.Extensions.Configuration
全局拦截器。使用AddAspectCore(Actioncf55c8c03f0256412fce639ce72d0b5d)
的重载方法,其中AspectCoreOptions提供InterceptorFactories注册全局拦截器:
services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(); });
带构造器参数的全局拦截器,在CustomInterceptorAttribute
中添加带参数的构造器:
public class CustomInterceptorAttribute : InterceptorAttribute { private readonly string _name; public CustomInterceptorAttribute(string name) { _name = name; } public async override Task Invoke(AspectContext context, AspectDelegate next) { try { Console.WriteLine("Before service call"); await next(context); } catch (Exception) { Console.WriteLine("Service threw an exception!"); throw; } finally { Console.WriteLine("After service call"); } } }
修改全局拦截器注册:
services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(args: new object[] { "custom" }); });
作为服务的全局拦截器。在ConfigureServices中添加:
services.AddTransient<CustomInterceptorAttribute>(provider => new CustomInterceptorAttribute("service"));
修改全局拦截器注册:
services.AddAspectCore(config => { config.InterceptorFactories.AddServiced<CustomInterceptorAttribute>(); });
作用于特定Service或Method的全局拦截器,下面的代码演示了作用于带有Service后缀的类的全局拦截器:
services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(method => method.DeclaringType.Name.EndsWith("Service")); });
使用通配符的特定全局拦截器:
services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(PredicateFactory.ForService("*Service")); });
在AspectCore中提供NonAspectAttribute来使得Service或Method不被代理:
[NonAspect] public interface ICustomService { void Call(); }
同时支持全局忽略配置,亦支持通配符:
services.AddAspectCore(config => { //App1命名空间下的Service不会被代理 config.NonAspectOptions.AddNamespace("App1"); //最后一级为App1的命名空间下的Service不会被代理 config.NonAspectOptions.AddNamespace("*.App1"); //ICustomService接口不会被代理 config.NonAspectOptions.AddService("ICustomService"); //后缀为Service的接口和类不会被代理 config.NonAspectOptions.AddService("*Service"); //命名为Query的方法不会被代理 config.NonAspectOptions.AddMethod("Query"); //后缀为Query的方法不会被代理 config.NonAspectOptions.AddMethod("*Query"); });
拦截器中的依赖注入。在拦截器中支持属性注入,构造器注入和服务定位器模式。
属性注入,在拦截器中拥有public get and set权限的属性标记[AspectCore.Abstractions.FromServices
](区别于Microsoft.AspNetCore.Mvc.FromServices
)特性,即可自动注入该属性,如:
public class CustomInterceptorAttribute : InterceptorAttribute { [AspectCore.Abstractions.FromServices] public ILogger<CustomInterceptorAttribute> Logger { get; set; } public override Task Invoke(AspectContext context, AspectDelegate next) { Logger.LogInformation("call interceptor"); return next(context); } }
构造器注入需要使拦截器作为Service,除全局拦截器外,仍可使用ServiceInterceptor使拦截器从DI中激活:
public interface ICustomService { [ServiceInterceptor(typeof(CustomInterceptorAttribute))] void Call(); }
服务定位器模式。拦截器上下文AspectContext可以获取当前Scoped的ServiceProvider:
public class CustomInterceptorAttribute : InterceptorAttribute { public override Task Invoke(AspectContext context, AspectDelegate next) { var logger = context.ServiceProvider.GetService<ILogger<CustomInterceptorAttribute>>(); logger.LogInformation("call interceptor"); return next(context); } }
使用Autofac和AspectCore。AspectCore原生支持集成Autofac,我们需要安装下面两个nuget packages:
PM> Install-Package Autofac.Extensions.DependencyInjection PM> Install-Package AspectCore.Extensions.Autofac
AspectCore提供RegisterAspectCore扩展方法在Autofac的Container中注册动态代理需要的服务,并提供AsInterfacesProxy和AsClassProxy扩展方法启用interface和class的代理。修改ConfigureServices方法为:
public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc(); var container = new ContainerBuilder(); container.RegisterAspectCore(); container.Populate(services); container.RegisterType<CustomService>().As<ICustomService>().InstancePerDependency().AsInterfacesProxy(); return new AutofacServiceProvider(container.Build()); }
以上是浅谈AspectCore_实用技巧的详细内容。更多信息请关注PHP中文网其他相关文章!

Microsoft的Windows112022Update(22H2)默认启用CoreIsolation的内存完整性保护。但是,如果您运行的是旧版本的操作系统,例如Windows112022Update(22H1),则需要手动打开此功能。在Windows11中开启CoreIsolation的内存完整性功能对于不了解核心隔离的用户,这是一个安全过程,旨在通过将Windows上的基本核心活动隔离在内存中来保护它们免受恶意程序的侵害。该进程与内存完整性功能相结合,可确保

电脑中core有两种意思:1、核心,也即内核,是CPU最重要的组成部分,CPU所有的计算、接受存储命令、处理数据都由核心执行;2、酷睿,core是英特尔的处理器名称,酷睿是英特尔公司继奔腾处理器之后推出的处理器品牌,目前已经发布了十二代酷睿处理器。
![如何修复 Windows 11 / 10 中的处理器热跳闸错误 [修复]](https://img.php.cn/upload/article/000/000/164/168169038621890.png)
大多数设备(例如笔记本电脑和台式机)长期被年轻游戏玩家和编码人员频繁使用。由于应用程序过载,系统有时会挂起。这使用户被迫关闭他们的系统。这主要发生在安装和玩重度游戏的玩家身上。当系统在强制关闭后尝试启动时,它会在黑屏上抛出一个错误,如下所示:以下是在此引导期间检测到的警告。这些可以在事件日志页面的设置中查看。警告:处理器热跳闸。按任意键继续。..当台式机或笔记本电脑的处理器温度超过其阈值温度时,总是会抛出这些类型的警告消息。下面列出了在Windows系统上发生这种情况的原因。许多繁重的应用程序在

随着.NETCore的推出,.NET开发者迎来了全新的机遇,可以在多个操作系统上轻松编写和运行.NET应用程序。本文将深入探讨如何利用.NETCore实现跨平台应用开发,并分享在Windows、Linux和macOS等操作系统上的最佳实践经验。一、准备开发环境要开始跨平台应用开发,首先需要为每个目标平台准备好开发环境。Windows在Windows上,你可以通过VisualStudio来安装.NETCoreSDK。安装完成后,你可以通过VisualStudio创建和运行.NETCore项目。Li

CORE币:值得长期持有吗?CORE币是一个基于工作量证明(PoW)共识机制的加密货币,由Core团队在2018年创立。其目标是建立一种安全、高效、可扩展的数字货币体系,被广泛应用于支付和价值储存。CORE币的设计旨在提供一种去中心化的支付解决方案,为用户提供更多的隐私保护和交易便利性。CORE币的优势安全:CORE币基于工作量证明共识机制,具有很强的安全性。高效:CORE币的交易速度快,每秒可处理数千笔交易。可扩展:CORE币的区块容量大,可支持大量交易。去中心化:CORE币是一个去中心化的加

在linux下core是一个内存映像,同时加上调试信息;在linux下遇到程序异常退出或者中止,我们都会使用core文件进行分析,其中包含了程序运行时的内存、寄存器、堆栈指针等信息,格式为ELF,可以理解是程序工作当前状态转储成一个文件。

Roughly one year after announcing the Core Ultra Series 1, also known as Meteor Lake, Intel follows up with the second generation. Core Ultra Series 2 aka Lunar Lake was already introduced at June's Computex. At IFA, the final launch of the Core Ultr

<ul><li><strong>点击进入:</strong>ChatGPT工具插件导航大全</li></ul>微软早在2021年9月就宣布推出配备最新英特尔处理器、NVIDIAGPU等的全新SurfaceLaptopStudio,但迄今为止该设备尚未在欧洲正式发行。已经泄露了该设备的最终到货日期,即2月22日,以及英国的定价。&a


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。