.netcore 中使用開源的AOP框架 AspectCore

AspectCore Project 介紹git

什麼是AspectCore Project ?

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

爲何要設計AspectCore ?

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

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

  • 提供抽象的Aop接口,基於該接口,能夠輕鬆的使用本身的代理類實現替換默認的實現
  • 框架不包含IoC,也不依賴具體的IoC實現,可使用Asp.Net Core的內置依賴注入或任何兼容 Asp.Net Core的第三方IoC來集成 AspectCore 到 Asp.Net Core 應用程序中
  • 高性能的異步攔截器系統
  • 靈活的配置系統
  • 基於Service的而非基於實現類的切面構造
  • 支持跨平臺的Asp.Net Core環境

 上面是一些概念介紹,關於AOP指的是面向切面編輯,其時咱們以前用過,只是沒注意而已,好比.netcore中的services中的方法,注入到服務中,權限過濾等均可以理解爲AOP.框架

 下面使用aspectcore寫一個注入的例子,能夠應用到驗證權限中。異步

  •  首先建立一個.netcore api項目,使用nuget添加AspectCore.Core、AspectCore.Extensions.DependencyInjection包的引用,我兩個用的是1.2.0版本

  • 建立自定義屬性類,繼承自AbstractInterceptorAttribute
using AspectCore.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AspectCore
{
    public class CustomInterceptorAttribute: AbstractInterceptorAttribute
    {
        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");
            }
        }

    }
}
  • 建立service類,添加屬性過濾
using AspectCore.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AspectCore.Services
{

    public interface ICustomService
    {
        [CustomInterceptor]
        void Call();
    }
    public class CustomService : ICustomService
    {
        public void Call()
        {
            Console.WriteLine("service calling...");
        }
    }
}
  • 建立Home控制器,添加測試Action
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AspectCore.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace AspectCore.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
      
        private readonly ICustomService _service;
        public HomeController(ICustomService service)
        {
            _service = service;
        }

       [HttpGet("getmsg")]
        public  void GetMsg()
        {
           
            _service.Call();
        }

    }
}
  • 在startup中注入服務,主要是ConfigureServices中作了修改
using System;
using AspectCore.Configuration;
using AspectCore.Extensions.DependencyInjection;
using AspectCore.Injector;
using AspectCore.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;


namespace AspectCore
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //根據屬性注入來配置全局攔截器
            services.ConfigureDynamicProxy(config => { config.Interceptors.AddTyped<CustomInterceptorAttribute>();//CustomInterceptorAttribute這個是須要全局攔截的攔截器
 }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); ; services.AddSingleton<ICustomService, CustomService>(); var serviceContainer = services.ToServiceContainer();//容器
            return serviceContainer.Build();

        }
           public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}
  • 最後運行項目,便可查看運行結果,網上有的說只能調用Control中Action方法爲virtual方法才能成功,實際上是錯誤的。
  • 目錄結構以下

相關文章
相關標籤/搜索