.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();

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

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


版权声明:本文为qq_43188456原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。