ASP.NET Core itself has integrated a lightweight IOC container

1, services.AddTransient <IApplicationService, ApplicationService> // services are created on every request , it is best used for lightweight stateless services (such as our ApplicationService and Repository Services)

2, services.AddScoped <IApplicationService, ApplicationService> // service each request is created across the entire life cycle times Request

3, services.AddSingleton <IApplicationService, ApplicationService> // Singleton ( singleton) service request is created the first time (or when we create an instance specified in ConfigureServices and operating method), of each subsequent request follow the service has been created . If the application developer needs singleton service scenario, please designed to allow the container to operate services of the service life cycle, instead of manually implemented singleton design pattern is then operated by the developer in the custom class.

Weights:

AddSingleton→AddTransient→AddScoped

1, AddSingleton life cycle: start of the project - the project is closed there will only be the equivalent of a static class  

2, AddScoped life cycle: request to start - end of the request in the request to obtain the objects are the same 

3, AddTransient life cycle: Request - (GC recycling - active release) acquired the object every time is not the same

Guess you like

Origin www.cnblogs.com/sylone/p/10949272.html