dotnetcore實現Aop


dotnetcore實現Aop

   Aop大家都不陌生,然而今天給大家不將講官方的filter,今天給大家分享一個輕量級的Aop解決方案(AspectCore)

什么是AspectCore

AspectCore是一個面向切面編程,基於.NetCore和.NetFramwork的擴平台框架,對方法攔截器、依賴項注入集成、web應用程序、數據驗證等提供核心支持。

AspectCore基本特性

  • 提供抽象的Aop接口,基於該接口可以輕松的使用自己的代理類實現替換默認的實現.

  • 框架不包含IoC,也不依賴具體IoC實現,可以使用Asp.Net Core的內置依賴注入或者任何兼容Asp.Net Core的第三方Ioc來繼承AspectCore到Asp.NetCore應用中

  • 高性能的異步攔截系統

  • 靈活的配置系統

  • 基於service的而非基於實現類的切面構造

  • 支持擴平台的Asp.Net Core環境

使用AspectCore

從NuGet中安裝AspectCore

AspectCore.Extensions.DependencyInjection

package

PM> Install-package AspectCore.Extensions.DependencyInjection

下面我創建了一個Api應用程序.

NuGet安裝

AspectCore.Configuration

package

PM> Install-package AspectCore.Configuration

下面我新建了一個攔截器 CustomInterceptorAttribute,繼承AbstractInterceptorAttribute(一般情況下繼承他即可),他實現IInterceptor接口AspectCore默認實現了基於Attribute的攔截器配置。

/// <summary>
///     自定義攔截器
/// </summary>
public class CustomInterceptorAttribute : AbstractInterceptorAttribute
{
    /// <summary>
    ///     實現抽象方法
    /// </summary>
    /// <param name="context"></param>
    /// <param name="next"></param>
    public override async Task Invoke(AspectContext context, AspectDelegate next)
    {
        try
        {
            Console.WriteLine("執行之前");
            await next(context);//執行被攔截的方法
        }
        catch (Exception)
        {
            Console.WriteLine("被攔截的方法出現異常");
            throw;
        }
        finally
        {
            Console.WriteLine("執行之后");
        }
    }
}

定義ICustomService接口和它的實現類CustomService:

public interface ICustomService
{
    DateTime GetDateTime();
}
public class CustomService : ICustomService
{
    public DateTime GetDateTime()
    {
        return DateTime.Now;
 }

}

在ValuesController注入ICustomService

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    private readonly ICustomService _icustomserveice;
    public ValuesController(ICustomService icustomService) {
        this._icustomserveice = icustomService;
    }
// GET api/values
[HttpGet]
public DateTime Get()
{
    return _icustomserveice.GetDateTime();
}

}

注冊ICustomService,並創建代理容器

 public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<ICustomService,CustomService>();
            services.AddMvc();
            //全局攔截器。使用AddDynamicProxy(Action<IAspectConfiguration>)的重載方法,其中IAspectConfiguration提供Interceptors注冊全局攔截器:
            services.ConfigureDynamicProxy(config=> {
                config.Interceptors.AddTyped<CustomInterceptorAttribute>();
            });
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            return services.BuildAspectInjectorProvider();
     }

作為服務的全局攔截器。在ConfigureServices中添加:

services.AddTransient<CustomInterceptorAttribute>(provider => new CustomInterceptorAttribute());

作用於特定ServiceMethod的全局攔截器,下面的代碼演示了作用於帶有Service后綴的類的全局攔截器:

 services.ConfigureDynamicProxy(config =>
            {
                config.Interceptors.AddTyped<CustomInterceptorAttribute>(method => method.DeclaringType.Name.EndsWith("Service"));
            });

通配符攔截器,匹配后綴為Service

 services.ConfigureDynamicProxy(config =>
            {
                config.Interceptors.AddTyped<CustomInterceptorAttribute>(Predicates.ForService("*Service"));
            });

在AspectCore中提供NonAspectAttribute來使得ServiceMethod不被代理:

   [NonAspect]
    DateTime GetDate();

全局配置忽略條件

      services.ConfigureDynamicProxy(config =>
        {
            //Namespace命名空間下的Service不會被代理
            config.NonAspectPredicates.AddNamespace("Namespace");
            //最后一級為Namespace的命名空間下的Service不會被代理
            config.NonAspectPredicates.AddNamespace("*.Namespace");
            //ICustomService接口不會被代理
            config.NonAspectPredicates.AddService("ICustomService");
            //后綴為Service的接口和類不會被代理
            config.NonAspectPredicates.AddService("*Service");
            //命名為Method的方法不會被代理
            config.NonAspectPredicates.AddMethod("Method");
            //后綴為Method的方法不會被代理
            config.NonAspectPredicates.AddMethod("*Method");
        });

AspectCore: [https://github.com/dotnetcore/AspectCore-Framework]
測試項目地址: [https://github.com/fhcodegit/DotNetAspectCore/tree/master]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM