淺談AspectCore_實用技巧
這篇文章主要介紹了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
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"); } } }
#
public interface ICustomService { [CustomInterceptor] void Call(); } public class CustomService : ICustomService { public void Call() { Console.WriteLine("service calling..."); } }
#
public class HomeController : Controller { private readonly ICustomService _service; public HomeController(ICustomService service) { _service = service; } public IActionResult Index() { _service.Call(); return View(); } }
public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddTransient<ICustomService, CustomService>(); services.AddMvc(); services.AddAspectCore(); return services.BuildAspectCoreServiceProvider(); }
PM> Install-Package AspectCore.Extensions.Configuration
AddAspectCore(Action的
重載方法,其中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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

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?x-oss-process=image/resize,m_fill,h_207,w_330)
大多數設備(例如筆記型電腦和桌上型電腦)長期被年輕遊戲玩家和編碼人員頻繁使用。由於應用程式過載,系統有時會掛起。這使用戶被迫關閉他們的系統。這主要發生在安裝和玩重遊戲的玩家身上。當系統在強制關閉後嘗試啟動時,它會在黑屏上拋出一個錯誤,如下所示:以下是在此引導期間檢測到的警告。這些可以在事件日誌頁面的設定中查看。警告:處理器熱跳閘。按任意鍵繼續。 ..當桌上型電腦或筆記型電腦的處理器溫度超過其閾值溫度時,總是會拋出這些類型的警告訊息。下面列出了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,可以理解是程式工作目前狀態轉儲成一個檔案。

在發布 Core Ultra Series 1(也稱為 Meteor Lake)大約一年後,英特爾又推出了第二代產品。 Core Ultra 系列 2 又名 Lunar Lake 已在 6 月的 Computex 上推出。在 IFA 上,Core Ultr 最終發布

在發布 Core Ultra Series 1(也稱為 Meteor Lake)大約一年後,英特爾又推出了第二代產品。 Core Ultra 系列 2 又名 Lunar Lake 已在 6 月的 Computex 上推出。在 IFA 上,Core Ultr 最終發布
