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接口。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>(); });
作用于特定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()); }
有问题反馈
如果您有任何问题,请提交 Issue 给我们。
AspectCore Project 项目地址: https://github.com/aspectcore
以上所述是小编给大家介绍的Asp.Net Core轻量级Aop解决方案:AspectCore,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对猪先飞网站的支持!
相关文章
- 这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
- 这篇文章主要介绍了Spring AOP 对象内部方法间的嵌套调用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-29
- 这篇文章主要介绍了.NET Core下使用Kafka的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
- 在开发过程中,使用Visual Studio的断点调试功能可以很方便帮我们调试发现程序存在的错误,同样Visual Studio也支持对SQL Server里面的存储过程进行调试,下面就让我们看看具体的调试方法。...2021-09-22
- 这篇文章主要介绍了详解.NET Core 3.0 里新的JSON API,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
ASP.NET Core根据环境变量支持多个 appsettings.json配置文件
这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22- 这篇文章主要介绍了记一次EFCore类型转换错误及解决方案,帮助大家更好的理解和学习使用asp.net core,感兴趣的朋友可以了解下...2021-09-22
详解ASP.NET Core 中基于工厂的中间件激活的实现方法
这篇文章主要介绍了ASP.NET Core 中基于工厂的中间件激活的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-22ASP.NET 2.0中的数据操作:使用两个DropDownList过滤的主/从报表
在前面的指南中我们研究了如何显示一个简单的主/从报表, 该报表使用DropDownList和GridView控件, DropDownList填充类别,GridView显示选定类别的产品. 这类报表用于显示具有...2016-05-19asp.net通过消息队列处理高并发请求(以抢小米手机为例)
这篇文章主要介绍了asp.net通过消息队列处理高并发请求(以抢小米手机为例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22- Underscore 是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象。这篇文章主要介绍了underscore源码分析相关知识,感兴趣的朋友一起学习吧...2016-01-02
ASP.NET单选按钮控件RadioButton常用属性和方法介绍
RadioButton又称单选按钮,其在工具箱中的图标为 ,单选按钮通常成组出现,用于提供两个或多个互斥选项,即在一组单选钮中只能选择一个...2021-09-22详解.NET Core 使用HttpClient SSL请求出错的解决办法
这篇文章主要介绍了.NET Core 使用HttpClient SSL请求出错的解决办法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2021-09-22ASP.NET中iframe框架点击左边页面链接 右边显示链接页面内容
这篇文章主要介绍了ASP.NET中iframe框架点击左边页面链接,右边显示链接页面内容的实现代码,感兴趣的小伙伴们可以参考一下...2021-09-22- ASP.NET Web API具有与ASP.NET MVC类似的编程方式,ASP.NET Web API不仅仅具有一个完全独立的消息处理管道,而且这个管道比为ASP.NET MVC设计的管道更为复杂,功能也更为强大。下面创建一个简单的Web API项目,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了ASP.NET连接MySql数据库的2个方法及示例,使用的是MySQL官方组件和ODBC.NET,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了Asp.Net使用Bulk实现批量插入数据的方法,对于进行asp.net数据库程序设计非常有借鉴价值,需要的朋友可以参考下...2021-09-22
在ASP.NET 2.0中操作数据之二十九:用DataList和Repeater来显示数据
本文主要讲解ASP.NET 2.0中如何使用DataList 和 Repeater 来呈现数据,DataList包含一个table标记,而Repeater不会添加任何额外的代码,个人在实际开发中更推荐使用Repeater。...2021-09-22- 这篇文章主要介绍了获取DataTable选择第一行某一列值,需要的朋友可以参考下...2021-09-22
- 这篇文章介绍了Asp.net动态生成html页面的方法,有需要的朋友可以参考一下...2021-09-22