A brief discussion on AspectCore_Practical skills
Jun 15, 2017 pm 01:49 PMThis article mainly introduces the Asp.Net Core lightweight Aop solution: AspectCore. Friends who need it can refer to it
What is AspectCore Project?
AspectCore Project is a lightweight Aop (Aspect-oriented programming) solution suitable for the Asp.Net Core platform. It better follows the modular development concept of Asp.Net Core. Using AspectCore can be more Easily build low-coupling, easily scalable web applications. AspectCore uses Emit to implement efficient dynamic proxy without relying on any third-party Aop library.
Start using AspectCore
Start Visual Studio. From the File menu, choose New > Project. Select the ASP.NET Core Web Application project template and create a new ASP.NET Core Web Application project.
Install AspectCore.Extensions.DependencyInjection package from Nuget:
- ##PM> Install-Package AspectCore.Extensions.DependencyInjection
- In general, you can use the abstract InterceptorAttribute custom attribute class, which implements the IInterceptor
public class CustomInterceptorAttribute : InterceptorAttribute { public async override Task Invoke(IAspectContext 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"); } } }
public interface ICustomService { [CustomInterceptor] void Call(); } public class CustomService : ICustomService { public void Call() { Console.WriteLine("service calling..."); } }
public class HomeController : Controller { private readonly ICustomService _service; public HomeController(ICustomService service) { _service = service; } public IActionResult Index() { _service.Call(); return View(); } }
public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddTransient<ICustomService, CustomService>(); services.AddMvc(); services.AddAspectCore(); return services.BuildAspectCoreServiceProvider(); }
PM> Install-Package AspectCore.Extensions.Configuration
overloaded method of
AddAspectCore(Action<AspectCoreOptions>), where AspectCoreOptions provides InterceptorFactories to register the global interceptor:
services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(); });
CustomInterceptorAttribute:
public class CustomInterceptorAttribute : InterceptorAttribute { private readonly string _name; public CustomInterceptorAttribute(string name) { _name = name; } 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"); } } }
services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(args: new object[] { "custom" }); });
services.AddTransient<CustomInterceptorAttribute>(provider => new CustomInterceptorAttribute("service"));
services.AddAspectCore(config => { config.InterceptorFactories.AddServiced<CustomInterceptorAttribute>(); });
services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(method => method.DeclaringType.Name.EndsWith("Service")); });
wildcard character Device:
services.AddAspectCore(config => { config.InterceptorFactories.AddTyped<CustomInterceptorAttribute>(PredicateFactory.ForService("*Service")); });
[NonAspect] public interface ICustomService { void Call(); }
services.AddAspectCore(config => { //App1命名空间下的Service不会被代理 config.NonAspectOptions.AddNamespace("App1"); //最后一级为App1的命名空间下的Service不会被代理 config.NonAspectOptions.AddNamespace("*.App1"); //ICustomService接口不会被代理 config.NonAspectOptions.AddService("ICustomService"); //后缀为Service的接口和类不会被代理 config.NonAspectOptions.AddService("*Service"); //命名为Query的方法不会被代理 config.NonAspectOptions.AddMethod("Query"); //后缀为Query的方法不会被代理 config.NonAspectOptions.AddMethod("*Query"); });
Dependency Injection in the interceptor. Supports property injection, constructor injection and service locator patterns in interceptors. Property injection, property tags with public get and
set permissions in the interceptor [AspectCore.Abstractions.FromServices] (different from
Microsoft.AspNetCore.Mvc. FromServices) feature, you can automatically inject this property, such as:
public class CustomInterceptorAttribute : InterceptorAttribute { [AspectCore.Abstractions.FromServices] public ILogger<CustomInterceptorAttribute> Logger { get; set; } public override Task Invoke(AspectContext context, AspectDelegate next) { Logger.LogInformation("call interceptor"); return next(context); } }
public interface ICustomService { [ServiceInterceptor(typeof(CustomInterceptorAttribute))] void Call(); }
public class CustomInterceptorAttribute : InterceptorAttribute { public override Task Invoke(AspectContext context, AspectDelegate next) { var logger = context.ServiceProvider.GetService<ILogger<CustomInterceptorAttribute>>(); logger.LogInformation("call interceptor"); return next(context); } }
PM> Install-Package Autofac.Extensions.DependencyInjection PM> Install-Package AspectCore.Extensions.Autofac
public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc(); var container = new ContainerBuilder(); container.RegisterAspectCore(); container.Populate(services); container.RegisterType<CustomService>().As<ICustomService>().InstancePerDependency().AsInterfacesProxy(); return new AutofacServiceProvider(container.Build()); }
The above is the detailed content of A brief discussion on AspectCore_Practical skills. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to enable Core Isolation's memory integrity feature in Windows 11
![How to Fix Processor Thermal Trip Error in Windows 11/10 [Fix]](https://img.php.cn/upload/article/000/000/164/168169038621890.png?x-oss-process=image/resize,m_fill,h_207,w_330)
How to Fix Processor Thermal Trip Error in Windows 11/10 [Fix]

Is CORE coin worth holding for the long term? Is CORE coin worth investing in?

.NET Core cross-platform application development practice: a seamless journey from Windows to Linux and macOS

Surface Laptop Studio is coming to Europe, here's how much it'll cost

IFA 2024 | Core Ultra Series 2: In Lunar Lake, Intel introduces its most efficient x86 CPU yet
