Aspectcore Project is a lightweight Aop (aspect-oriented programming) solution for the ASP. NET core platform, which better adheres to the modular development concept of ASP. Using Aspectcore makes it easier to build low-coupling, easy-to-scale Web applications. Aspectcore uses emit to implement efficient dynamic proxies without relying on any third-party AOP libraries.
Start Visual Studio. From the File menu, choose New > Project. Select the ASP. NET core Web application project template to create a new ASP. NET core Web application project.
To install the package from Nuget AspectCore.Extensions.DependencyInjection
:
pm> install-package AspectCore.Extensions.DependencyInjection
-
Under normal circumstances, you can use the abstract interceptorattribute
custom attribute class, which implements the Iinterceptor
interface. The Aspectcore default implements a Attribute
-based interceptor configuration. Our custom interceptors look 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 ICustomService
the interface and its implementation class CustomService
:
public interface icustomservice{ [custominterceptor] void Call ();} public class customservice:icustomservice{public void Call () { Console.WriteLine ("Service Calling ...") ; }}
In the HomeController
injection ICustomService
:
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 ConfigureServices
configure the container to create the proxy type in:
Public IServiceProvider configureservices (iservicecollection services) { services. Addtransient<icustomservice, customservice> (); Services. Addmvc (); Services. Addaspectcore (); Return services. Buildaspectcoreserviceprovider ();}
Interceptor configuration. Install the package First AspectCore.Extensions.Configuration
:
Pm> Install-package AspectCore.Extensions.Configuration
Global Interceptor. AddAspectCore(Action<AspectCoreOptions>)
the overloaded method used, which AspectCoreOptions
provides the registration of the InterceptorFactories
Global Interceptor:
Services. Addaspectcore (config = = { config. Interceptorfactories.addtyped<custominterceptorattribute> (); });
Global interceptor with constructor parameters, CustomInterceptorAttribute
add a constructor with parameters in:
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"); } }}
To modify the global Interceptor registration:
Services. Addaspectcore (config =>{ config. Interceptorfactories.addtyped<custominterceptorattribute> (args:new object[] {"Custom"});
As a global interceptor for the service. In the ConfigureServices
add:
Services. Addtransient<custominterceptorattribute> (Provider = new Custominterceptorattribute ("service"));
To modify the global Interceptor registration:
Services. Addaspectcore (config =>{ config. Interceptorfactories.addserviced<custominterceptorattribute> ();});
Acting on a specific Service
or Method
global Interceptor, the following code demonstrates a global interceptor that acts on a Service
class with a suffix:
Services. Addaspectcore (config =>{ config. Interceptorfactories.addtyped<custominterceptorattribute> (method = method). DeclaringType.Name.EndsWith ("Service");});
A specific global interceptor that uses wildcards:
services. Addaspectcore (config =>{config. Interceptorfactories.addtyped<custominterceptorattribute> (Predicatefactory.forservice ("*Service");});
-
Provides nonaspectattribute
in Aspectcore to make Service
or Method
non-proxied:
[nonaspect]public interface icustomservice{void Call ();}
The supports both global ignore configuration and wildcard characters:
services. Addaspectcore (config = service under the {//app1) namespace will not be proxy config. Nonaspectoptions.addnamespace ("App1"); Service in the last-level APP1 namespace will not be proxy config. Nonaspectoptions.addnamespace ("*. App1 "); The Icustomservice interface is not proxy config. Nonaspectoptions.addservice ("Icustomservice"); Interfaces and classes that are suffixed with service will not be proxy config. Nonaspectoptions.addservice ("*service"); The method named query is not proxy config. Nonaspectoptions.addmethod ("Query"); The method with the suffix query will not be proxy config. Nonaspectoptions.addmethod ("*query"); });
The dependency injection in the interceptor. attribute injection, constructor injection, and service locator mode are supported in interceptors.
attribute injection, which has the attribute tag (distinguished from) attribute of the permission in the Interceptor, is public get and set
[AspectCore.Abstractions.FromServices]
Microsoft.AspNetCore.Mvc.FromServices
automatically injected into the 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 to next (context);} }
Constructor injection requires the interceptor to Service
be used, in addition to the global interceptor, to enable the Interceptor to be ServiceInterceptor
activated from di:
public interface icustomservice{ [Serviceinterceptor (typeof (Custominterceptorattribute))] void call ();
Service Locator mode. The interceptor context AspectContext
can get the current scoped ServiceProvider
:
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); }}
Use Autofac
and AspectCore
. Aspectcore native support for integrated AUTOFAC, we need to install the following two NuGet packages:
pm> install-package autofac.extensions.dependencyinjectionpm> install-package ASPECTCORE.EXTENSIONS.AUTOFAC
Aspectcore provides RegisterAspectCore
extension methods to register the Container
services required by dynamic agents in AUTOFAC, and provides AsInterfacesProxy
and AsClassProxy
extends methods to enable agents for interface and class. Modify ConfigureServices
the method to:
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 ());}
If you have any questions, please submit Issue to us.
Aspectcore Project Projects Address:
At last...
Looking for a job, welcome to recommend. Net/.net Core back-end development positions, coordinates Shanghai, can be private messages or email.