我是靠谱客的博主 风趣诺言,这篇文章主要介绍什么是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:

    复制代码
    1
    PM> Install-Package AspectCore.Extensions.DependencyInjection
    登录后复制
  • 在一般情况下可以使用抽象的InterceptorAttribute自定义特性类,它实现IInterceptor接口。AspectCore默认实现了基于Attribute的拦截器配置。我们的自定义拦截器看起来像下面这样:

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    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:

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public interface ICustomService { [CustomInterceptor] void Call(); } public class CustomService : ICustomService { public void Call() { Console.WriteLine("service calling..."); } }
    登录后复制
  • HomeController中注入ICustomService:

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class HomeController : Controller { private readonly ICustomService _service; public HomeController(ICustomService service) { _service = service; } public IActionResult Index() { _service.Call(); return View(); } }
    登录后复制
  • 注册ICustomService,接着,在ConfigureServices中配置创建代理类型的容器:

    复制代码
    1
    2
    3
    4
    5
    6
    7
    public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddTransient<ICustomService, CustomService>(); services.AddMvc(); services.AddAspectCore(); return services.BuildAspectCoreServiceProvider(); }
    登录后复制
  • 拦截器配置。首先安装AspectCore.Extensions.Configuration package:

    复制代码
    1
    PM> Install-Package AspectCore.Extensions.Configuration
    登录后复制

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

    复制代码
    1
    2
    3
    4
    services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(); });
    登录后复制

    带构造器参数的全局拦截器,在CustomInterceptorAttribute中添加带参数的构造器:

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    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"); } } }
    登录后复制

    修改全局拦截器注册:

    复制代码
    1
    2
    3
    4
    services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(args: new object[] { "custom" }); });
    登录后复制

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

    复制代码
    1
    services.AddTransient<CustomInterceptorAttribute>(provider => new CustomInterceptorAttribute("service"));
    登录后复制

    修改全局拦截器注册:

    复制代码
    1
    2
    3
    4
    services.AddAspectCore(config => { config.InterceptorFactories.AddServiced<CustomInterceptorAttribute>(); });
    登录后复制

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

    复制代码
    1
    2
    3
    4
    services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(method => method.DeclaringType.Name.EndsWith("Service")); });
    登录后复制

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

    复制代码
    1
    2
    3
    4
    services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(PredicateFactory.ForService("*Service")); });
    登录后复制
  • 在AspectCore中提供NonAspectAttribute来使得ServiceMethod不被代理:

    复制代码
    1
    2
    3
    4
    5
    [NonAspect] public interface ICustomService { void Call(); }
    登录后复制

    同时支持全局忽略配置,亦支持通配符:

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    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)特性,即可自动注入该属性,如:

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    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中激活:

    复制代码
    1
    2
    3
    4
    5
    public interface ICustomService { [ServiceInterceptor(typeof(CustomInterceptorAttribute))] void Call(); }
    登录后复制

    服务定位器模式。拦截器上下文AspectContext可以获取当前Scoped的ServiceProvider

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    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:

    复制代码
    1
    2
    PM> Install-Package Autofac.Extensions.DependencyInjection PM> Install-Package AspectCore.Extensions.Autofac
    登录后复制

    AspectCore提供RegisterAspectCore扩展方法在Autofac的Container中注册动态代理需要的服务,并提供AsInterfacesProxyAsClassProxy扩展方法启用interface和class的代理。修改ConfigureServices方法为:

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    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 项目地址:

最后。。。

正在找工作,欢迎推荐.NET/.NET Core后端开发职位,坐标上海,可私信或Email。

以上就是什么是AspectCore Project ?的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是风趣诺言最近收集整理的关于什么是AspectCore Project ?的全部内容,更多相关什么是AspectCore内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(140)

评论列表共有 0 条评论

立即
投稿
返回
顶部