Asp.Net Core輕量級Aop解決方案:AspectCore

来源:http://www.cnblogs.com/liuhaoyang/archive/2017/06/06/aspectcore-introduction-1.html
-Advertisement-
Play Games

什麼是AspectCore Project ? "AspectCore Project" 是適用於Asp.Net Core 平臺的輕量級 Aop(Aspect oriented programming) 解決方案,它更好的遵循Asp.Net Core的模塊化開發理念,使用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(Action<AspectCoreOptions>)的重載方法,其中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>();
    });

    作用於特定ServiceMethod的全局攔截器,下麵的代碼演示了作用於帶有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來使得ServiceMethod不被代理:

    [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);
        }
    }
  • 使用AutofacAspectCore。AspectCore原生支持集成Autofac,我們需要安裝下麵兩個nuget packages:

    PM> Install-Package Autofac.Extensions.DependencyInjection
    PM> Install-Package AspectCore.Extensions.Autofac

    AspectCore提供RegisterAspectCore擴展方法在Autofac的Container中註冊動態代理需要的服務,並提供AsInterfacesProxyAsClassProxy擴展方法啟用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());
    }

有問題反饋

如果您有任何問題,請提交 Issue 給我們。
AspectCore Project 項目地址: https://github.com/aspectcore

最後。。。

正在找工作,歡迎推薦.NET/.NET Core後端開發職位,坐標上海,可私信或Email


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • mysqlbinlog --database=資料庫名 --start-date="2017-06-01 5:00:00" --stop-date="2017-06-11 10:00:00" var/mysql-bin.000001 | mysql -u root -p123456 -f ...
  • 範例: .tar 解包:tar -xvf FileName.tar 打包:tar -cvf FileName.tar DirName (註:tar是打包,不是壓縮!) .gz 解壓1:gunzip FileName.gz 解壓2:gzip -d FileName.gz 壓縮:gzip FileNam ...
  • SQL 語句中In 和 Where 的含義不同。應用解釋如下:1、如需有條件地從表中選取、刪除、更新數據時,使用Where;2、In只作為Where條件子句下的一個運算符,除了In之外還有Between、Like、=、>、>=、<、<=等運算符。下麵舉例說明:1、查詢名字為A和B的學生,則語句為Se ...
  • 我們時長在批量插入時,需要獲取插入數據的id. 這樣: 這樣是沒問題的. 但是有時候牽扯到批量插入,並且獲取插入的id 這樣寫: 這樣運行後就會出現異常了. 這是因為你用的mybatis版本過低.比如我用的是3.2.2版本,這是mybatis的一個bug. 如果你換成3.4.4版本就沒有問題了. 上 ...
  • 一、變數 1.變數的命名規則:以字母或下劃線開頭,後面跟數字,字母或下劃線,最好不要隨便命名,要做到看見變數名能猜出其含義 2.變數賦值: x=100 echo $x 刪除變數:unset x 3.定義變數名的邊界用大括弧 [root@bogon ~]# egon_salary=20000[root ...
  • linux下部署php項目環境可以分為兩種,一種使用Apache,php,mysql的壓縮包安裝,一種用yum命令進行安裝。 使用三種軟體的壓縮包進行安裝,需要手動配置三者之間的關係。apache和php之間的配置沒有什麼難度,但是和mysql進行配置的時候就需要對php的瞭解了。 以下是用yum在 ...
  • 聲明:本文為轉載內容,感謝原作者辛勤勞動。原鏈接為:http://www.cnblogs.com/chinahbzm/articles/1423875.html 1)建立空連接: net use \\IP\ipc$ "" /user:"" (一定要註意:這一行命令中包含了3個空格) 2)建立非空連接 ...
  • 引言 最新有一個winform項目使用的是DevExpress的控制項,所以最近都在摸索使用這套控制項,實在是佩服整套控制項的強大,同時代碼寫起來也簡潔。客戶有一個需求,希望報表結果能在外接的大屏幕上定時滾動。這個報表我們使用的控制項就是GridControl,查詢結果一屏不能顯示完全,增加一個定時器,指定 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...