.net Core原生的依赖注入加AOP

因为.net Core原生过滤器只能在controller层里面能用,在普通类里面无法使用。
经过我搜了大量的文章与整理才得出结论:

安装包:Castle.Core

1、创建一个拦截器

using Castle.DynamicProxy; 
namespace AOPDemo.Model
{
    public class TestInterceptor : StandardInterceptor
    {
       
        /// <summary>
        /// 执行前
        /// </summary>
        /// <param name=invocation></param>
        protected override void PreProceed(IInvocation invocation)
        {
            Console.WriteLine(invocation.Method.Name + 执行前,入参: + string.Join(,, invocation.Arguments));
        }
 
        /// <summary>
        /// 执行中
        /// </summary>
        /// <param name=invocation></param>
        protected override void PerformProceed(IInvocation invocation)
        {
            Console.WriteLine(invocation.Method.Name + 执行中);
           base.PerformProceed(invocation);
        }
 
        /// <summary>
        /// 执行后
        /// </summary>
        /// <param name=invocation></param>
        protected override void PostProceed(IInvocation invocation)
        {
            Console.WriteLine(invocation.Method.Name + 执行后,返回值: + invocation.ReturnValue);
        }
    }
}

2、创建普通服务:

namespace AOPDemo.Service
{
    public interface ITestService
    {
        void test();
    }
}


namespace AOPDemo.Service
{
    public class TestService : ITestService
    {
        public void test()
        {
            Console.WriteLine(调用了服务!!!!!);
        }
    }
}

3、接口类

using AOPDemo.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
 
namespace AOPDemo.Controllers
{
    [Route(api/[controller])]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly ITestService _testService;
        public HomeController(ITestService testService) {
            this._testService = testService;
        }
        [HttpGet]
        public void Get() {
            _testService.test();
        }
    }
}

4、Program

using AOPDemo.Model;
using AOPDemo.Service;
using Castle.DynamicProxy;
 
var builder = WebApplication.CreateBuilder(args);
 
// Add services to the container.
 
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
 
var service = builder.Services;
 
service.Add(new ServiceDescriptor(typeof(TestService),typeof(TestService),ServiceLifetime.Transient));
ProxyGenerator generator = new ProxyGenerator();
object Factory(IServiceProvider provider) {
    var service = provider.GetService(typeof(TestService));
    return generator.CreateInterfaceProxyWithTarget(typeof(ITestService),service,new TestInterceptor());
};
service.Add(new ServiceDescriptor(typeof(ITestService), Factory,ServiceLifetime.Transient));
 
 
var app = builder.Build();
 
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
 
app.UseHttpsRedirection();
 
app.UseAuthorization();
 
app.MapControllers();
 
app.Run();

运行效果:
在这里插入图片描述

本文章的代码地址:本文章的代码地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值