分享

(6).NET CORE微服务 Micro

 兰亭文艺 2020-02-25

AOP 框架基础

要求懂的知识:AOP、Filter、反射(Attribute)。

如果直接使用 Polly,那么就会造成业务代码中混杂大量的业务无关代码。我们使用 AOP (如果不了解 AOP,请自行参考网上资料)的方式封装一个简单的框架,模仿 Spring cloud 中的 Hystrix。

需要先引入一个支持.Net Core 的 AOP,我们用.Net Core 下的 AOP 框架是AspectCore(国产,动态织入),其他要不就是不支持.Net Core,要不就是不支持对异步方法进行拦截 MVC Filter。

GitHub:https://github.com/dotnetcore/AspectCore-Framework

Install-Package AspectCore.Core -Version 0.5.0

这里只介绍和我们相关的用法:

1、编写拦截器CustomInterceptorAttribute 一般继承自AbstractInterceptorAttribute

复制代码
复制代码
public class CustomInterceptorAttribute:AbstractInterceptorAttribute{ //每个被拦截的方法中执行  public async override Task Invoke(AspectContext context, AspectDelegate next)  {
    try    {      Console.WriteLine('执行之前');
      await next(context);//执行被拦截的方法    }    catch (Exception)    {      Console.WriteLine('被拦截的方法出现异常');
      throw;    }    finally    {      Console.WriteLine('执行之后');    }  }}
复制代码
复制代码

2、编写需要被代理拦截的类

在要被拦截的方法上标注CustomInterceptorAttribute 。类需要是public类,方法如果需要拦截就是虚方法,支持异步方法,因为动态代理是动态生成被代理的类的动态子类实现的。

复制代码
复制代码
public class Person{    [CustomInterceptor]              public virtual void Say(string msg)    {         Console.WriteLine('service calling...'+msg);    }}
复制代码
复制代码

3、通过AspectCore创建代理对象

复制代码
复制代码
ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder(); using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build()){ Person p = proxyGenerator.CreateClassProxy<Person>(); p.Say('rupeng.com');}Console.ReadKey();
复制代码
复制代码

注意p指向的对象是AspectCore生成的Person的动态子类的对象,直接new Person是无法被拦截的。

研究AOP细节

拦截器中Invoke方法下的参数AspectContext的属性的含义:

Implementation  实际动态创建的Person子类的对象。

ImplementationMethod就是Person子类的Say方法

Parameters 方法的参数值。

Proxy==Implementation:当前场景下

ProxyMethod==ImplementationMethod:当前场景下

ReturnValue返回值

ServiceMethod是Person的Say方法

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多