netcore使用AutoFac实现AOP

原文:netcore使用AutoFac实现AOP

第一步,添加程序集引用

在Nuget中搜索autofac找到Autofac.Extras.DynamicProxy并安装。

第二步:添加拦截器

/// <summary>
    /// 拦截器(实现 Castle.DynamicProxy.IInterceptor)接口
    /// </summary>
    public class CustomAutoFacAOPInterception : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            Console.WriteLine("执行之前可以写日志、参数检查……");       //可以捕获异常
            invocation.Proceed();
            Console.WriteLine("执行之后可以写日志……");
        }
    }

    /// <summary>
    /// person服务接口
    /// </summary>
    public interface IPerson {
        string Speak();
    }

    /// <summary>
    /// 接口的实现类
    /// 注意,与Unity实现aop不同,autofac是作用于实现类而不是接口
    /// </summary>
    [Intercept(typeof(CustomAutoFacAOPInterception))]
    public class Person : IPerson
    {
        public string Speak()
        {
            return "你好,我是一个Person";
        }
    }

第三步,在注册模块注册拦截器并启用AOP拦截

/// <summary>
    /// AutoFac注册模块
    /// </summary>
    public class CustomAutoFacModule: Autofac.Module
    {
       /// <summary>
       /// 重写父类的Load方法
       /// </summary>
       /// <param name="builder"></param>
        protected override void Load(ContainerBuilder builder)
        {
            //1、注册拦截器
            builder.Register(a => new CustomAutoFacAOPInterception());
            //2、设置该类型允许AOP拦截
            builder.RegisterType<Person>().As<IPerson>().EnableInterfaceInterceptors().SingleInstance();
            builder.RegisterType<UserProvider>().As<IUserService>().SingleInstance();//感叹,这语法,真的是不能再爽了
            //后面可以注册好多类型……
            //后面可以注册好多类型……
            //后面可以注册好多类型……
            //后面可以注册好多类型……
        }
    }

第四步,调用

 public class InterceptDemoController : Controller
    {
        private IPerson _personService = null;
        /// <summary>
        /// 构造注入
        /// </summary>
        /// <param name="person"></param>
        public InterceptDemoController(IPerson person)
        {
            _personService = person;
        }
        public IActionResult Index()
        {
            string rel = _personService.Speak();
            return Content(rel);
        }
    }

原文地址:https://www.cnblogs.com/lonelyxmas/p/12549286.html

时间: 2024-08-03 15:08:04

netcore使用AutoFac实现AOP的相关文章

.NET Core下自带容器IServiceCollection以及AutoFac以及AutoFac中AOP简介

https://www.cnblogs.com/artech/p/net-core-di-01.html 大内老A的在.NET Core下对这些的介绍,有一系列文章 https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html https://www.cnblogs.com/artech/p/dependency-injection-in-asp-net-core.html https://www.zybuluo.com/dasajia2la

Autofac实现AOP拦截

本文主要是详解一下在ASP.NET Core中,采用替换后的Autofac来实现AOP拦截. Aspect Oriented Programming(AOP),面向切面编程,是一个比较热门的话题.AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. 引入类库 nuget命令如下: Install-Package Autofac.Extras.DynamicProxy -Version 4.5.0 复制代码

netcore使用Autofac实现依赖注入

原文:netcore使用Autofac实现依赖注入 第一步,添加程序集引用 在nuget中搜索"autofac",添加下图中的两个程序集.他们的作用分别是autofac的核心库和依赖倒置的实现库. 第二步,注册服务 修改Startup类的ConfigureServices方法 public IServiceProvider ConfigureServices(IServiceCollection services) { services.Configure<CookiePolic

Autofac的AOP面向切面编程研究

*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !important; } .markdown-body a:not([href]) { color: inherit; text-decoration: none; } .markdown-body .anchor { float: left; padding-right: 4px; margin-left: -2

Autofac与AOP功能例子

using Autofac.Extras.DynamicProxy; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace aopTest { [Intercept(typeof(CallLogger))] public interface IProduct { string Title { get;

ASP.NETCore使用AutoFac依赖注入

实现代码 1.新建接口类:IRepository.cs,规范各个操作类的都有那些方法,方便管理. using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace CMS.Entity.Interfaces { public interface IRepository<T> where T:class { /

.NET 通过 Autofac 和 DynamicProxy 实现AOP

什么是AOP?引用百度百科:AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.实现AOP主要由两种方式,一种是编译时静态植入,优点是效率高,缺点是缺乏灵活性,.net下postsharp为代表者(这个是收费的).另一种方式是动态代理,优缺点与前者相反,动态为目标类型创建代理,通过代理调用实现拦截.AOP能做什么,常见的用例是事务处理.日志记录等等.下面就讲讲Autofac怎么实现AOP,Aut

从壹开始前后端分离【 .NET Core2.0 Api + Vue 3.0 + AOP + 分布式】框架之九 || 依赖注入IoC学习 + AOP界面编程初探

代码已上传Github,文末有地址 说接上文,上回说到了<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之八 || API项目整体搭建 6.3 异步泛型+依赖注入初探>,后来的标题中,我把仓储两个字给去掉了,因为好像大家对这个模式很有不同的看法,嗯~可能还是我学艺不精,没有说到其中的好处,现在在学DDD领域驱动设计相关资料,有了好的灵感再给大家分享吧. 到目前为止我们的项目已经有了基本的雏形,后端其实已经可以搭建自己的接口列表了,框架已

asp.net core learn

.NET Core WebApi RESTful规范 RESTful API 最佳实践 理解RESTful架构 接口版本控制 Support multiple versions of ASP.NET Core Web API ASP.NET Core API 版本控制 配置使用流程 使用方法 Startup类ConfigureServices方法中加入services.AddApiVersioning() Startup类Configure方法中加入app.UseApiVersioning()