zoukankan      html  css  js  c++  java
  • 6.1 自定义abp拦截器示例

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

    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)

  • 相关阅读:
    Linux服务器修改时区
    Linux磁盘IO查看
    passwd: 鉴定令牌操作错误
    Kubernetes 使用Nginx-Ingress实现蓝绿发布/金丝雀发布/AB测试【转】
    MySQL count 浅析【转】
    k8s pv,pvc无法删除问题【转】
    K8S 上部署 Redis-cluster 三主三从 集群【转】
    XtraBackup 备份加速【转】
    Gitlab+DRBD 高可用方案【转】
    Linux网卡bond的七种模式详解【转】
  • 原文地址:https://www.cnblogs.com/zhiyong-ITNote/p/11978453.html
Copyright © 2011-2022 走看看