标签:not nts 依赖注入 apt LEDE end Edito cores orm
using AspectCore.Extensions.DependencyInjection; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; namespace WebApplication4 { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) // for aspcectcore .UseServiceProviderFactory(new AspectCoreServiceProviderFactory()); } }
System.NotSupportedException:“ConfigureServices returning an System.IServiceProvider isn‘t supported
using AspectCore.DynamicProxy; using System; using System.Threading.Tasks; namespace WebApplication4.Aop { public class CustomInterceptorAttribute : AbstractInterceptorAttribute { public async override Task Invoke(AspectContext context, AspectDelegate next) { try { Console.WriteLine("Before service call"); await next(context); } catch (Exception) { Console.WriteLine("Service threw an exception!"); throw; } finally { Console.WriteLine("After service call"); } } } }
using System.Collections.Generic; using WebApplication4.Aop; namespace WebApplication4.App { public class StudentRepository : IStudentRepository { [CustomInterceptor] public List<Student> GetAllStudents() { var students = new List<Student>(); students.Add(new Student { Name = "Panxixi", Age = 11 }); students.Add(new Student { Name = "Liuchuhui", Age = 12 }); return students; } } }
ASP.NET Core 3 使用原生 依赖注入 集成 AspectCore ,实现 AOP 功能
标签:not nts 依赖注入 apt LEDE end Edito cores orm
原文地址:https://www.cnblogs.com/panxixi/p/11905006.html