首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >6.1 自定义abp拦截器示例

6.1 自定义abp拦截器示例

作者头像
Ryan_OVO
发布2023-10-19 19:35:02
发布2023-10-19 19:35:02
6170
举报
文章被收录于专栏:程序随笔程序随笔

一个简单、基于AbpInterceptor的拦截器示例:

代码语言:javascript
复制
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.DynamicProxy;

namespace ConsoleApp1
{
    public interface ICanLogOnObject
    {
        List<string> Logs { get; }
    }

    public class SimpleAsyncInterceptor : AbpInterceptor
    {
        public override void Intercept(IAbpMethodInvocation invocation)
        {
            (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add($"{GetType().Name}_Intercept_BeforeInvocation");
            invocation.ProceedAsync();
            (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add($"{GetType().Name}_Intercept_AfterInvocation");
        }

        public override async Task InterceptAsync(IAbpMethodInvocation invocation)
        {
            await Task.Delay(5);
            (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add($"{GetType().Name}_InterceptAsync_BeforeInvocation");
            await invocation.ProceedAsync();
            (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add($"{GetType().Name}_InterceptAsync_AfterInvocation");
            await Task.Delay(5);
        }
    }

    public class SimpleInterceptionTargetClass : ICanLogOnObject
    {
        public List<string> Logs { get; } = new List<string>();

        public virtual void DoIt()
        {
            Logs.Add("ExecutingDoIt");
        }

        public virtual int GetValue()
        {
            Logs.Add("ExecutingGetValue");
            return 42;
        }

        public virtual async Task<int> GetValueAsync()
        {
            Logs.Add("EnterGetValueAsync");
            await Task.Delay(5);
            Logs.Add("MiddleGetValueAsync");
            await Task.Delay(5);
            Logs.Add("ExitGetValueAsync");
            return 42;
        }

        public virtual async Task DoItAsync()
        {
            Logs.Add("EnterDoItAsync");
            await Task.Delay(5);
            Logs.Add("MiddleDoItAsync");
            await Task.Delay(5);
            Logs.Add("ExitDoItAsync");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 服务容器
            var services = new ServiceCollection();
            services.AddTransient<SimpleAsyncInterceptor>();
            services.AddTransient<SimpleInterceptionTargetClass>();
            services.OnRegistred(register =>
            {
                // 添加拦截器
                if (typeof(SimpleInterceptionTargetClass) == register.ImplementationType)
                {
                    register.Interceptors.Add<SimpleAsyncInterceptor>();
                }
            });

            var application = services.AddApplication<AbpTestModule>(options =>
            {
                options.UseAutofac();
            });

            var rootServiceProvider = services.BuildServiceProviderFromFactory();
            var testServiceProvider = rootServiceProvider.CreateScope();
            var ServiceProvider = testServiceProvider.ServiceProvider;

            application.Initialize(ServiceProvider);

            // 拦截器 代码 ↓
            var target = ServiceProvider.GetRequiredService<SimpleInterceptionTargetClass>();
            target.DoIt();

            foreach (var log in target.Logs)
            {
                Console.WriteLine(log);
            }

            Console.WriteLine("Done");
            Console.Read();
        }
    }
}

拦截器调用顺序,可参考打上断点调试分析:

AutofacRegistration.Populate(内部调用Autofac.Extras.DynamicProxy) --> SimpleAsyncInterceptor.Intercept --> CastleAbpMethodInvocationAdapter.Proceed(内部调用Castle.DynamicProxy)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档