In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares to you is about the service life cycle in ASP.NET Core. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
ASP.NET Core supports the dependency injection (DI) software design pattern, which allows us to register services, control how they are instantiated, and inject them into different components. Some services can be instantiated in a short period of time and are only available in specific components and requests; some instances are instantiated only once and are available throughout the application lifecycle. This is the service lifecycle available in ASP.NET Core. There are three types of service lifecycles. Let's introduce each of them.
Singleton (single case)
Create a single instance of the service class, store it in memory, and reuse it throughout the application. We can use Singleton to instantiate expensive services. You can register the Singleton service using the AddSingleton method, as follows:
Services.AddSingleton (); Scoped (within scope)
Each request creates a service instance. All middleware, MVC controllers, and so on that participate in processing a single request will get the same instance. The entity Framework context (Entity Framework context) is a good scenario for using Scoped services. We can register the Scoped service using the AddScoped method, as follows:
Services.AddScoped (); Transient (temporary)
Transient lifecycle services are created each time they are requested. This lifecycle is best suited for lightweight, stateless services. We can register the Transient service using the AddTransient method, as follows:
Services.AddTransient ()
If you want to visualize the above concepts, here is a chart for your quick reference.
Translator's note:
In the figure, the Instance with different background colors (shades) represents different service instances.
The above is the service life cycle in ASP.NET Core, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.