MethodInterceptor

news/2025/2/22 6:01:56/

目录

1 MethodInterceptor

1.1 /// This will be called via Reflection

1.2 HandleAsync

  1. MethodInterceptor
    1.     /// This will be called via Reflection

    /// </summary>

    /// <typeparam name="TResult"></typeparam>

    /// <param name="filterAttributes"></param>

    /// <param name="exceptionFilterAttributes"></param>

    /// <param name="methodExecutingContext"></param>

    /// <returns></returns>

    private async Task<TResult> HandleAsyncWithType<TResult>(IReadOnlyList<MethodFilterAttribute> filterAttributes, IReadOnlyList<ExceptionFilterAttribute> exceptionFilterAttributes, MethodExecutingContext methodExecutingContext)

        {

        foreach (var f in filterAttributes)

        {

        try

        {

        if (methodExecutingContext.Result == null) await f.OnMethodExecutingAsync(methodExecutingContext).ConfigureAwait(false);

        }

        catch (Exception ex)

        {

        var exContext = new MethodExceptionContext(ex, methodExecutingContext);

        await HandleExceptionAsync(exceptionFilterAttributes, exContext);

        if (!exContext.Handled)

        {

        throw;

        }

        }

        }

        var reversedFilterAttributes = filterAttributes.Reverse();

        var methodExecutedContext = new MethodExecutedContext(methodExecutingContext);

        foreach (var f in reversedFilterAttributes)

        {

        try

        {

        await f.OnMethodExecutedAsync(methodExecutedContext).ConfigureAwait(false);

        }

        catch (Exception ex)

        {

        var exContext = new MethodExceptionContext(ex, methodExecutedContext);

        await HandleExceptionAsync(exceptionFilterAttributes, exContext);

        if (!exContext.Handled)

        {

        throw;

        }

        }

        }

        if (methodExecutedContext.Result != null && methodExecutedContext.Result is TResult result)

        {

        return result;

        }

return default(TResult);

        }

    1. HandleAsync

        private async Task HandleAsync(IReadOnlyList<MethodFilterAttribute> filterAttributes, IReadOnlyList<ExceptionFilterAttribute> exceptionFilterAttributes, MethodExecutingContext methodExecutingContext)

        {

        foreach (var f in filterAttributes)

        {

        try

        {

        if (methodExecutingContext.Result == null) await f.OnMethodExecutingAsync(methodExecutingContext).ConfigureAwait(false);

        }

        catch (Exception ex)

        {

        var exContext = new MethodExceptionContext(ex, methodExecutingContext);

        await HandleExceptionAsync(exceptionFilterAttributes, exContext);

        if (!exContext.Handled)

        {

        throw;

        }

        }

        }

        var reversedFilterAttributes = filterAttributes.Reverse();

        var methodExecutedContext = new MethodExecutedContext(methodExecutingContext);

        foreach (var f in reversedFilterAttributes)

        {

        try

        {

        await f.OnMethodExecutedAsync(methodExecutedContext).ConfigureAwait(false);

        }

        catch (Exception ex)

        {

        var exContext = new MethodExceptionContext(ex, methodExecutedContext);

        await HandleExceptionAsync(exceptionFilterAttributes, exContext);

        if (!exContext.Handled)

        {

        throw;

        }

        }

        }

        }


http://www.ppmy.cn/news/991854.html

相关文章

指针的基础应用(数组的颠倒和排序,二维数组的表示)

1.数组的颠倒&#xff1a;若有10个数字&#xff0c;那么数组的颠倒即 a[0]与a[9]交换,a[1]与a[8]交换&#xff0c;a[2]与a[7]交换&#xff0c;......a[4]与a[5]交换&#xff0c;所以到a[4]就颠倒完毕&#xff0c;即 (n-1)/2 若不用指针代码如下 #include<stdio.h>voi…

常见的几种排序

&#x1f436;博主主页&#xff1a;ᰔᩚ. 一怀明月ꦿ ❤️‍&#x1f525;专栏系列&#xff1a;线性代数&#xff0c;C初学者入门训练&#xff0c;题解C&#xff0c;C的使用文章&#xff0c;「初学」C &#x1f525;座右铭&#xff1a;“不要等到什么都没有了&#xff0c;才下…

#P1002. [NOIP2009普及组] 细胞分裂

Hanks 博士是 BT&#xff08;Bio-Tech&#xff0c;生物技术&#xff09;领域的知名专家。现在&#xff0c;他正在为一个细胞实验做准备工作&#xff1a;培养细胞样本。 Hanks 博士手里现在有 NN 种细胞&#xff0c;编号从 1 \sim N1∼N&#xff0c;一个第 ii 种细胞经过 11 秒…

c语言重定向stdout 到 /dev/null 及恢复

------------------------------------------------------------ author: hjjdebug date: 2023年 07月 27日 星期四 17:49:49 CST descriptor: c语言重定向stdout 到 /dev/null 及恢复 ------------------------------------------------------------ #include <stdio.h&g…

Stable Diffusion ControlNet 完全指南

ControlNet 是 Stable Diffusion中的一种扩展模型&#xff0c;通过这种扩展模型&#xff0c;我们能够将参考图像的构图&#xff08;compositions &#xff09;或者人体姿势迁移到目标图像。 资深 Stable Diffusion 用户都知道&#xff0c;很难精准控制Stable Diffusion生成的图…

深入理解pytorch库的dockerfile

目录 0. dockerfile命令1. 使用指令的注意点3. 参考4. 存在的问题 0. dockerfile命令 ? 1 2 3 4 5 6 7 8 9 10 11 12 FROM # 基础镜像&#xff0c;一切从这里开始构建 MAINTAINER # 镜像是谁写的&#xff0c;姓名邮箱 RUN # 镜像构建的时候需要…

网络应用技术师技能考试试题(二)

♥️作者&#xff1a;小刘在C站 ♥️个人主页&#xff1a; 小刘主页 ♥️努力不一定有回报&#xff0c;但一定会有收获加油&#xff01;一起努力&#xff0c;共赴美好人生&#xff01; ♥️学习两年总结出的运维经验&#xff0c;以及思科模拟器全套网络实验教程。专栏&#xf…

华为OD机试真题 Java 实现【输出指定字母在字符串的中的索引】【2023 B卷 100分】,附详细解题思路

目录 专栏导读一、题目描述二、输入描述三、输出描述说明&#xff1a; 四、解题思路五、Java算法源码六、效果展示1、输入2、输出3、说明 华为OD机试 2023B卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#x…