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

View: 57373|Reply: 4

[Source] ASP.NET Core (VI) DI manually obtains the method of injecting objects

[Copy link]
Posted on 2021-3-19 15:13:15 | | |
Tutorial on ASP.NET Core Dependency Injection, Control Inversion (DI IOC):

ASP.NET Core dependency injection uses the IOC container that comes with it
https://www.itsvse.com/thread-7562-1-1.html


Review:

ASP.NET Core (five) is based on CAP distributed transactions
https://www.itsvse.com/thread-9593-1-1.html

ASP.NET Core(4) filter unified ModelState model validation
https://www.itsvse.com/thread-9589-1-1.html

ASP.NET Core (iii) Dynamically create instances using ActivatorUtilities
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core (2) Restart the application by code
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core (1) uses Redis caching
https://www.itsvse.com/thread-9393-1-1.html
Dependency injection in a nutshell:

Dependency injection (DI) is a technique that implements loose coupling between an object and its collaborators or dependencies. Provide these objects that the class uses to perform its operations to the class in some way, rather than directly instantiating collaborators or using static references.

ASP.NET Core DI generally uses constructor injection to obtain objects, such as after configuring the ConfigureServices injection, it can be obtained in the following ways:


What if you manually get the injection object?

The first way to get it(Sometimes you can't get it,Not recommended):


The second way to get itRecommended use):

Note: It should be noted that using ServiceLocator.Instance.GetService<T>(); ,Only objects injected by AddTransient and AddSingleton can be fetched, and cannot obtain the injected object of AddScoped (unique in the request lifecycle), not because it cannot be obtained, but because the acquired object is not obtained by the constructor, that is, the acquired object is not shared, and the usage scenario is such as IUnitOfWork.

So how do you manually get the injection objects during the request lifecycle? Here's how:


or

The IHttpContextAccessor interface is under the Microsoft.AspNetCore.Http.AbstractionsNuget package.




Previous:HttpClient POST directly to send byte byte (gzip) compression requests
Next:The difference between the Roslyn MSBuild compiler
Posted on 2021-9-22 20:43:14 |
Learn to learn...
Posted on 2021-9-22 20:55:17 |
Learn to learn...
Posted on 2022-6-17 16:05:39 |
Learn how to use it
 Landlord| Posted on 2024-12-30 17:09:09 |
The console injects the IHttpContextAccessor interface:



.NET/C# console services are registered with Consul service discovery
https://www.itsvse.com/thread-10899-1-1.html
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