日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

[Asp.Net Core轻量级Aop解决方案]AspectCore Project 介绍

發布時間:2023/12/4 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Asp.Net Core轻量级Aop解决方案]AspectCore Project 介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AspectCore Project 介紹

什么是AspectCore Project ?

AspectCore Project 是適用于Asp.Net Core 平臺的輕量級 Aop(Aspect-oriented programming) 解決方案,它更好的遵循Asp.Net Core的模塊化開發理念,使用AspectCore可以更容易構建低耦合、易擴展的Web應用程序。

為什么要設計AspectCore ?

在傳統.Net Framework和Asp.Net Framework中,我們使用Castle DynamicProxy 或者CLR提供的 Remoting.Proxies 可以輕松的實現 Aop 來分離關注點從而降低業務邏輯和基礎框架功能的耦合。然而在Asp.Net Core中,不僅缺乏細粒度的Aop支持(Middleware和Filter都是Asp.Net Core的內置Aop實現,但僅適合在Web層使用),Castle也遲遲未能友好的支持Asp.Net Core。

因此 AspectCore 提供了一個全新的輕量級和模塊化的Aop解決方案,下面是AspectCore的基本特性:

  • 提供抽象的Aop接口,基于該接口,可以輕松的使用自己的代理類實現替換默認的實現

  • 框架不包含IoC,也不依賴具體的IoC實現,可以使用Asp.Net Core的內置依賴注入或任何兼容 Asp.Net Core的第三方IoC來集成 AspectCore 到 Asp.Net Core 應用程序中

  • 高性能的異步攔截器系統

  • 靈活的配置系統

  • 基于Service的而非基于實現類的切面構造

  • 支持跨平臺的Asp.Net Core環境

AspectCore Project 架構設計

從上圖可以看出,AspectCore Abstractions 是AspectCore Project的核心,向下通過IoC來集成到Asp.Net Core應用程序中,向上提供配置系統,動態代理系統,模型驗證系統以及更多的擴展系統。

目前已完成的組件包括:

  • AspectCore.Lite.Abstractions 提供Aop的抽象接口

  • AspectCore.Lite.Abstractions.Resolution 默認的Aop實現

  • AspectCore.Lite.Container.DependencyInjection AspectCore的DependencyInjection支持

  • AspectCore.Lite.Container.Autofac AspectCore的Autofac支持

在Asp.Net Core應用程序中開始使用AspectCore

  • 啟動 Visual Studio。從 File 菜單, 選擇 New > Project。選擇 ASP.NET Core Web Application 項目模版,創建新的 ASP.NET Core Web Application 項目。

  • 從 Nuget 安裝 AspectCore.Lite.Container.DependencyInjection package:

    PM> Install-Package AspectCore.Lite.Container.DependencyInjection -Pre

    在攔截器系統中,AspectCore定義了IInterceptor接口,它聲明了一個返回值為Task的異步執行方法:

    namespace AspectCore.Lite.Abstractions{public interface IInterceptor{ ? ?Task Invoke(IAspectContext context, AspectDelegate next); } }

    然而在一般情況下可以使用另一個抽象的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();
    return new AspectCoreServiceProviderFactory().CreateServiceProvider(services); }

    有問題反饋

    如果您有任何問題,請提交 Issue 給我們。
    AspectCore Project 項目地址: https://github.com/aspectcore
    AspectCore Project 文檔地址: https://docs.aspectcore.org(文檔在持續更新中..)

  • 原文地址:http://www.cnblogs.com/liuhaoyang/p/aspectcore-introduction.html


    .NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注

    總結

    以上是生活随笔為你收集整理的[Asp.Net Core轻量级Aop解决方案]AspectCore Project 介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。