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/01 Report--
This article introduces the knowledge of "what is the life cycle of ASP.NET Core service". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Preface
Register each application's service in the container in the ConfigureServices method, and Asp.Core can provide three service lifecycles for each application:
Transient (temporarily): a new instance is created for each request. This lifecycle is best suited for lightweight, stateless services.
Scoped (scope): only one instance is initialized in the same scope, which can be understood to mean that only one instance is created for each request, and the same request is in a scope.
Singleton (singleton): only one instance is created throughout the application life cycle, and the same instance is used for each subsequent request. If the application requires singleton behavior, it is recommended that the service container manage the lifecycle of the service instead of implementing the singleton pattern in its own class.
two。 Case demonstration of Service Lifecycle and Registration options
To demonstrate the difference between lifecycle and registration options, consider the following interface to represent a task as an operation with a unique identifier OperationId. Depending on how the following interfaces configure the lifecycle of the operation service, the container provides the same or different service instances when the class is requested:
Public interface IOperation {Guid OperationId {get;}} public interface IOperationTransient: IOperation {} public interface IOperationScoped: IOperation {} public interface IOperationSingleton: IOperation {} public interface IOperationSingletonInstance: IOperation {}
The above four service interfaces are implemented in the Operation class. A GUID is automatically generated when the Operation class is called. Here is the implementation of the Operation class:
Public class Operation: IOperationTransient, IOperationScoped, IOperationSingleton, IOperationSingletonInstance {public Operation (): this (Guid.NewGuid ()) {} public Operation (Guid id) {OperationId = id;} public Guid OperationId {get; private set;}}
Another OperationService service instance is registered, and when an OperationService instance is requested through dependency injection, it will receive a new instance of each service or an existing instance based on the life cycle of the dependent service (Operation). The role of the OperationService service is to call the Operation class for the second time to see the scope change of the Operation class instance.
Public class OperationService {public OperationService (IOperationTransient transientOperation, IOperationScoped scopedOperation, IOperationSingleton singletonOperation, IOperationSingletonInstance instanceOperation) {_ transientOperation = transientOperation; _ scopedOperation = scopedOperation; _ singletonOperation = singletonOperation; _ singletonInstanceOperation = instanceOperation;} public IOperationTransient _ transientOperation {get;} public IOperationScoped _ scopedOperation {get;} public IOperationSingleton _ singletonOperation {get;} public IOperationSingletonInstance _ singletonInstanceOperation {get;}
Then register the instances of each lifecycle in the Startup.ConfigureServices () service container:
Public void ConfigureServices (IServiceCollection services) {services.AddTransient (); services.AddScoped (); services.AddSingleton (); services.AddSingleton (new Operation (Guid.Empty)); / / OperationService depends on each of the other Operation types. Services.AddTransient ();}
Then call the OnGet method output in the IndexModel module to observe the change of the attribute OperationId value of the IOperation and OperationService classes:
Public class IndexModel: PageModel {public OperationService _ operationService {get;} public IOperationTransient _ transientOperation {get;} public IOperationScoped _ scopedOperation {get;} public IOperationSingleton _ singletonOperation {get;} public IOperationSingletonInstance _ singletonInstanceOperation {get;} public IndexModel (OperationService operationService, IOperationTransient transientOperation, IOperationScoped scopedOperation, IOperationSingleton singletonOperation, IOperationSingletonInstance singletonInstanceOperation) {_ operationService = operationService; _ transientOperation = transientOperation; _ scopedOperation = scopedOperation; _ singletonOperation = singletonOperation _ singletonInstanceOperation = singletonInstanceOperation;} public void OnGet () {Console.WriteLine ("IOperation Operation:"); Console.WriteLine ("temporary:" + _ transientOperation.OperationId.ToString ()); Console.WriteLine ("scope:" + _ scopedOperation.OperationId.ToString ()); Console.WriteLine ("Singleton:" + _ singletonOperation.OperationId.ToString ()) Console.WriteLine ("instance:" + _ singletonInstanceOperation.OperationId.ToString ()); Console.WriteLine ("OperationService Operation:"); Console.WriteLine ("temporarily:" + _ operationService._transientOperation.OperationId.ToString ()); Console.WriteLine ("scope:" + _ operationService._scopedOperation.OperationId.ToString ()); Console.WriteLine ("Singleton:" + _ operationService._singletonOperation.OperationId.ToString ()) Console.WriteLine ("instance:" + _ operationService._singletonInstanceOperation.OperationId.ToString ());}}
Execute the IndexModel class output result:
The figure is summarized as follows:
2.1 Transient (temporary): a new instance is created each time the service is invoked. That is, a dependent object Operation class is instantiated in the local method or property of the IndexModel class (in this case, the OnGet method). The pseudo code is:
Public class IndexModel: when PageModel {public void OnGet () {/ / calls the IndexModel class, the Operation class / / the first OperationService operationService=new OperationService (); / the second IOperationTransient TransientOperation=new Operation ();}} is instantiated twice
2.2 Scoped (scope): the object instance is the same within an Action, but a new instance is generated for each request. This is equivalent to instantiating the dependent object Operation class once in the global of the IndexModel class. The pseudo code is:
OperationService operationService = null;public IndexModel () {operationService = new OperationService (); operationService._scopedOperation = new Operation ();} public void OnGet () {operationService._scopedOperation.OperationId; IOperationScoped operationScoped = operationService._scopedOperation; operationScoped.OperationId}
2.3 Singleton (singleton): initialize the same instance for the first time, and use the same instance for each subsequent request. It is equivalent to instantiating the instance only once in the whole application Application, the common singleton pattern.
The lifecycle flowchart is as follows:
That's all for "what is the life cycle of ASP.NET Core services?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.