AspectCore Project 介紹git
AspectCore Project 是適用於Asp.Net Core 平臺的輕量級 Aop(Aspect-oriented programming) 解決方案,它更好的遵循Asp.Net Core的模塊化開發理念,使用AspectCore能夠更容易構建低耦合、易擴展的Web應用程序。github
在傳統.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。api
所以 AspectCore 提供了一個全新的輕量級和模塊化的Aop解決方案,下面是AspectCore的基本特性:app
Service
的而非基於實現類的切面構造上面是一些概念介紹,關於AOP指的是面向切面編輯,其時咱們以前用過,只是沒注意而已,好比.netcore中的services中的方法,注入到服務中,權限過濾等均可以理解爲AOP.框架
下面使用aspectcore寫一個注入的例子,能夠應用到驗證權限中。異步
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"); } } } }
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..."); } } }
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(); } } }
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(); } } }