asp.net core 3.1 应用Aufac 进行AOP 的三种方式1 基于接口AOP

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 System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Aufac
{
    public interface IAufacInterFace3
    {
        string GetName();

    }
    public class AufacInterFace3: IAufacInterFace3
    {
        /// <summary>
        /// 基于接口的AOP
        /// </summary>
        /// <returns></returns>
        public   string GetName()
        {
            Debug.WriteLine("this is getname");
            return "this is getname";
        }
    }
}

4.1.2配置startup 注入到服务

       public void ConfigureContainer(ContainerBuilder builder)
        {

            //基于接口的AOP
            builder.RegisterType<WebApplication1.Aufac.CacheIntercept>();
            builder.RegisterType<WebApplication1.Aufac.AufacInterFace3>().As<WebApplication1.Aufac.IAufacInterFace3>()
              .AsImplementedInterfaces()
             .InterceptedBy(typeof(WebApplication1.Aufac.CacheIntercept))
             .EnableInterfaceInterceptors();
}

4.1.3在代码里调用

AufacInterFace3 发现已经进行了拦截处理
调用代码如下
  WebApplication1.Aufac.IAufacInterFace3 face3;

        public WeatherForecastController(WebApplication1.Aufac.IAufacInterFace3 interFace3
            )
        {
            face3 = interFace3;
        }
        [HttpGet]
        //[Caching(AbsoluteExpiration =2,keyname ="id",keyvaluename ="id")]
        public virtual string Get()
        {
            face2.GetName();
            return DateTime.Now.ToString();
        }

 

asp.net core 3.1 应用Aufac 进行AOP 的三种方式1 基于接口AOP

上一篇:微信企业号开发之weixin://preInjectJSBridge/fail


下一篇:cube.js trino driver