• asp.net core 基于autofac 实现AOP 拦截 之 第二种方式 -基于class


    Autofac 官方地址:https://autofaccn.readthedocs.io/en/latest/advanced/interceptors.html

    (1)引用 nuget :Autofac.Extensions.DependencyInjection  Autofac.Extras.DynamicProxy

    (2)修改入口文件

    public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    //改用Autofac来实现依赖注入
                    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                        
                    });

    (3)修改startup 文件

         //autofac 新增
            public ILifetimeScope AutofacContainer { get; private set; }
      public void ConfigureContainer(ContainerBuilder builder)
            {
    }

    (4)创建AOP拦截文件

    using Castle.DynamicProxy;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace WebApplication1.Aufac
    {
        public class CacheIntercept : IInterceptor
        {
            public CacheIntercept()
            { }
            public void Intercept(IInvocation invocation)
            {
                var method = invocation.MethodInvocationTarget ?? invocation.Method;
                Debug.WriteLine("before");
                invocation.Proceed();
                Debug.WriteLine("end");
            }
        }
    }

    (4)应用拦截效果

          4-1 使用类进行AOP

           4.1.1 创建以下类文件

    using Autofac.Extras.DynamicProxy;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace WebApplication1.Aufac
    {
        /// <summary>
        /// 类的AOP方式1
        /// </summary>
        [Intercept("datacache")]
        public class AufacClass1
        {
            /// <summary>
            /// 必须是virtual
            /// </summary>
            /// <returns></returns>
            public virtual string GetName()
            {
                Debug.WriteLine("this is getname");
                return "this is getname";
            }
        }
    }

    4.1.2配置startup 注入到服务

            //autofac 新增
            public void ConfigureContainer(ContainerBuilder builder)
            {
               builder.RegisterType<WebApplication1.Aufac.AufacClass1>().EnableClassInterceptors();
                builder.Register(c => new WebApplication1.Aufac.CacheIntercept()).Named<IInterceptor>("datacache");
    
    }

    4.1.3在代码里调用 发现已经进行了拦截处理

    调用代码如下

    WebApplication1.Aufac.AufacClass1 face1;
    public WeatherForecastController(WebApplication1.Aufac.AufacClass1 interFace1
    )
    {
    face1 = interFace1;
    }
    [HttpGet]
    //[Caching(AbsoluteExpiration =2,keyname ="id",keyvaluename ="id")]
    public  string Get()
    {
    face1.GetName();
    return DateTime.Now.ToString();
    }

  • 相关阅读:
    PHP 命名空间
    使用 htaccess 重写 url,隐藏查询字符串
    HTML 长文本换行
    Mac OS X 上的Apache配置
    无法debug断点跟踪JDK源代码——missing line number attributes的解决方法
    根据多条件删除还能这样写
    wm_concat()函数
    spring 事务-使用@Transactional 注解(事务隔离级别)
    spring 中常用的两种事务配置方式以及事务的传播性、隔离级别
    oracle 中SQL 语句开发语法 SELECT INTO含义
  • 原文地址:https://www.cnblogs.com/qiejinxing/p/14652185.html
Copyright © 2020-2023  润新知