This article is a mirror article of machine translation, please click here to jump to the original article.

View: 80854|Reply: 10

[.NET Core] ASP.NET Core uses Autofac to implement AOP interception

[Copy link]
Posted on 5/8/2019 3:09:15 PM | | | |
ASP.NET Core dependency injection uses the IOC container that comes with it
https://www.itsvse.com/thread-7562-1-1.html
(Source: Architect_Programmer)

ASP.NET Core uses Autofac to implement IOC injection
https://www.itsvse.com/thread-7563-1-1.html
(Source: Architect_Programmer)

ASP.NET Core uses Autofac to implement AOP interception
https://www.itsvse.com/thread-7566-1-1.html
(Source: Architect_Programmer)

This article mainly explains in detail the use of the replaced Autofac to implement AOP interception in ASP.NET Core.

Aspect Oriented Programming (AOP) is a hot topic. The main purpose of AOP is to extract the cut-to-face surface in the business processing process, which is faced with a certain step or stage in the processing process to obtain the isolation effect of low coupling between the parts of the logic process.

Introduce class libraries

The nuget command is as follows:


Autofac is used to implement AOP

First, we create an intercept class with the following code:

Here, you need to inherit the IInterceptor and then implement its Intercept method: We directly output the intercepted content to the debug window (official project: Please operate the interception according to the business):

Here I use ILogger to record the operation, as follows:

ASP.NET Core ILogger Log Usage Tutorial
https://www.itsvse.com/thread-7565-1-1.html
(Source: Architect_Programmer)

Find the services we want to intercept, TestService1 and TestService2, and we do it in two ways.

TestService1: Intercepted by a feature
TestService2: Intercept by configuration

The code is as follows:

In the Startup editing method ConfigureServices, block the TestService2 service by configuring it as follows:

The important code is as follows:

Note here that you must add it after the service you injectEnableInterfaceInterceptorsto turn on your interception

The controller code is as follows:

Start the project via the dotnet run command, access the URL, and the console output log is as follows:



We found that the return value intercepted by AOP is the same as that returned to the web page, so we have completed AOP interception with Autofac.

(End)




Previous:ASP.NET Core ILogger Log Usage Tutorial
Next:Django Enterprise Development Practical Guide to Efficient Python Web Framework Author Hu Yang
Posted on 5/9/2019 8:18:56 AM |
It is best to have the source code of the instance
 Landlord| Posted on 5/9/2019 9:11:27 AM |
Summer Posted on 2019-5-9 08:18
It is best to have the source code of the instance

The project contains sensitive information, so I won't send it, and the tutorial is already very detailed
Posted on 5/9/2019 11:15:17 AM |
Posted on 5/14/2019 4:39:00 PM |
Learned,
Posted on 11/22/2021 2:24:55 PM |
Learn AOP
Posted on 12/17/2021 2:20:20 PM |
Thank you for sharing, let's learn
Posted on 12/17/2021 2:21:41 PM |
Is there a framework for this to be implemented? The owner writes his own code? What else is a high-quality response...
Posted on 12/24/2021 4:23:00 PM |
It's so good to write, I learned
Posted on 1/10/2022 11:12:24 AM |
Learn to learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com