千家信息网

什么是AspectCore Project

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,这篇文章给大家分享的是有关什么是AspectCore Project的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。什么是AspectCore Project ?Aspect
千家信息网最后更新 2025年02月01日什么是AspectCore Project

这篇文章给大家分享的是有关什么是AspectCore Project的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

什么是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();  services.AddMvc();  services.AddAspectCore();  return services.BuildAspectCoreServiceProvider();}

拦截器配置。首先安装AspectCore.Extensions.Configuration package:

PM> Install-Package AspectCore.Extensions.Configuration

全局拦截器。使用AddAspectCore(Action)的重载方法,其中AspectCoreOptions提供InterceptorFactories注册全局拦截器:

 services.AddAspectCore(config => {   config.InterceptorFactories.AddTyped(); });

带构造器参数的全局拦截器,在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(args: new object[] { "custom" });});

作为服务的全局拦截器。在ConfigureServices中添加:

services.AddTransient(provider => new CustomInterceptorAttribute("service"));

修改全局拦截器注册:

services.AddAspectCore(config =>{  config.InterceptorFactories.AddServiced();});

作用于特定Service或Method的全局拦截器,下面的代码演示了作用于带有Service后缀的类的全局拦截器:

services.AddAspectCore(config =>{  config.InterceptorFactories.AddTyped(method => method.DeclaringType.Name.EndsWith("Service"));});

使用通配符的特定全局拦截器:

services.AddAspectCore(config =>{  config.InterceptorFactories.AddTyped(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 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>();    logger.LogInformation("call interceptor");    return next(context);  }}

使用Autofac和AspectCore。AspectCore原生支持集成Autofac,我们需要安装下面两个nuget packages:

PM> Install-Package Autofac.Extensions.DependencyInjectionPM> 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().As().InstancePerDependency().AsInterfacesProxy();  return new AutofacServiceProvider(container.Build());}

感谢各位的阅读!关于"什么是AspectCore Project"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

拦截器 全局 代理 方法 属性 接口 构造器 支持 服务 配置 后缀 作用 内容 动态 参数 定位器 更多 模式 特性 空间 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 辽宁正宗服务器机柜云服务器 网络安全黑龙江院校 图片是存在数据库还是路径 手机游戏软件开发的前景 大型网络数据库 人民检察院加强网络安全管理 软件开发合同书封面 浦东新区数据软件开发价钱 为什么要建立参展商数据库 网络安全动态报告 数据库检测点技术 东方国信数据库占比多少 政务网络安全措施三个 本地文件如何复制远程服务器 网络安全例会心得 北邮考研网络安全好不好考 服务器管理终端快捷方式 魔兽怀旧服各服务器注册人数 达梦数据库批量建表 工模软件开发工程师 手机软件开发工具软件 网站后台数据库管理 网络安全法 网络安全责任书 我国医疗保险财政支出数据库 北京云网无限网络技术 杭州韵晖网络技术有限公司 以下网络安全理解错误的是 有关介绍网络安全的书籍 运维部门网络安全心得 学校知网数据库多久一更新
0