In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about what AspectCore Project is. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
What is AspectCore Project?
AspectCore Project is a lightweight Aop (Aspect-oriented programming) solution for Asp.Net Core platform. It better follows the modular development concept of Asp.Net Core. Using AspectCore, it is easier to build low-coupling, easy-to-expand Web applications. AspectCore uses Emit to implement efficient dynamic proxies without relying on any third-party Aop libraries.
Activate the use of AspectCore
Start Visual Studio. From the File menu, select New > Project. Select the ASP.NET Core Web Application project template to 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 property class, which implements the IInterceptor interface. AspectCore implements Attribute-based interceptor configuration by default. Our custom interceptor looks like this:
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");}
Define the ICustomService interface and its implementation class CustomService:
Public interface ICustomService {[CustomInterceptor] void Call ();} public class CustomService: ICustomService {public void Call () {Console.WriteLine ("service calling...");}}
Inject ICustomService into HomeController:
Public class HomeController: Controller {private readonly ICustomService _ service; public HomeController (ICustomService service) {_ service = service;} public IActionResult Index () {_ service.Call (); return View ();}
Register ICustomService, and then configure the container in ConfigureServices to create the proxy type:
Public IServiceProvider ConfigureServices (IServiceCollection services) {services.AddTransient (); services.AddMvc (); services.AddAspectCore (); return services.BuildAspectCoreServiceProvider ();}
Interceptor configuration. Install AspectCore.Extensions.Configuration package first:
PM > Install-Package AspectCore.Extensions.Configuration
Global interceptor. Use the overloaded method of AddAspectCore (Action), where AspectCoreOptions provides InterceptorFactories to register the global interceptor:
Services.AddAspectCore (config = > {config.InterceptorFactories.AddTyped ();})
Global interceptor with constructor parameters, add constructors with parameters to 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") }}}
Modify the global interceptor registration:
Services.AddAspectCore (config = > {config.InterceptorFactories.AddTyped (args: new object [] {custom "});})
As the global interceptor of the service. Add to the ConfigureServices:
Services.AddTransient (provider = > new CustomInterceptorAttribute ("service"))
Modify the global interceptor registration:
Services.AddAspectCore (config = > {config.InterceptorFactories.AddServiced ();})
The following code demonstrates a global interceptor that acts on a class with a Service suffix that works on a specific Service or Method:
Services.AddAspectCore (config = > {config.InterceptorFactories.AddTyped (method = > method.DeclaringType.Name.EndsWith ("Service"));})
A specific global interceptor that uses wildcards:
Services.AddAspectCore (config = > {config.InterceptorFactories.AddTyped (PredicateFactory.ForService ("* Service");})
Provide NonAspectAttribute in AspectCore to prevent Service or Method from being proxied:
[NonAspect] public interface ICustomService {void Call ();}
Global ignore configuration and wildcard characters are also supported:
Services.AddAspectCore (config = > {/ / Service under the App1 namespace will not be proxied by config.NonAspectOptions.AddNamespace ("App1"); / / Service under the namespace with the last level App1 will not be proxied config.NonAspectOptions.AddNamespace ("* .App1"); / / the ICustomService interface will not be proxied by config.NonAspectOptions.AddService ("ICustomService"); / / interfaces and classes with the suffix Service will not be proxied config.NonAspectOptions.AddService ("* Service") / / methods named Query will not be proxied config.NonAspectOptions.AddMethod ("Query"); / / methods with suffixes of Query will not be proxied config.NonAspectOptions.AddMethod ("* Query");})
Dependency injection in the interceptor. Property injection, constructor injection, and service locator patterns are supported in interceptors.
Attribute injection, the attribute tag [AspectCore.Abstractions.FromServices] (different from Microsoft.AspNetCore.Mvc.FromServices) that has public get and set permission in the interceptor can be injected automatically, such as:
Public class CustomInterceptorAttribute: InterceptorAttribute {[AspectCore.Abstractions.FromServices] public ILogger Logger {get; set;} public override Task Invoke (AspectContext context, AspectDelegate next) {Logger.LogInformation ("call interceptor"); return next (context);}}
Constructor injection requires the interceptor to act as a Service, and in addition to the global interceptor, you can still use ServiceInterceptor to activate the interceptor from the DI:
Public interface ICustomService {[ServiceInterceptor (typeof (CustomInterceptorAttribute))] void Call ();
Service locator mode. Interceptor context AspectContext can get the ServiceProvider of the current Scoped:
Public class CustomInterceptorAttribute: InterceptorAttribute {public override Task Invoke (AspectContext context, AspectDelegate next) {var logger = context.ServiceProvider.GetService (); logger.LogInformation ("call interceptor"); return next (context);}}
Use Autofac and AspectCore. AspectCore natively supports integration of Autofac. We need to install the following two nuget packages:
PM > Install-Package Autofac.Extensions.DependencyInjectionPM > Install-Package AspectCore.Extensions.Autofac
AspectCore provides the services required by RegisterAspectCore extension methods to register dynamic agents in Autofac's Container, and provides AsInterfacesProxy and AsClassProxy extension methods to enable agents for interface and class. Modify the ConfigureServices method to:
Public IServiceProvider ConfigureServices (IServiceCollection services) {services.AddMvc (); var container = new ContainerBuilder (); container.RegisterAspectCore (); container.Populate (services); container.RegisterType (). As (). InstancePerDependency (). AsInterfacesProxy (); return new AutofacServiceProvider (container.Build ());} Thank you for reading! This is the end of this article on "what is AspectCore Project?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.