Transient: Each time a getservice creates a new instance
Scoped: Only one instance is initialized within the same scope, which can be understood as (only one instance is created per request level, and the same HTTP request is within a scope)
Singleton: Only one instance is created throughout the application life cycle
The above description of the comparative abstract, not easy to understand, with examples to explain the more intuitive.
The following is a demonstration with specific examples.
Define three empty interfaces: Iarticleservice, Iproductservice, Iuserservice
Then define three implementations: Articleservice, Productservice, UserService
1. Injecting the interface and implementation into the DI container
Adding code to the Configureservices method of the startup class
Public void configureservices (iservicecollection services) { services. Addmvc (); Services. Configure<Test> (configuration.getsection ("Test")); // Demo life cycle Services. Addtransient<iuserservice, userservice>(); Services. addscoped<iarticleservice, articleservice>(); Services. Addsingleton<iproductservice, productservice>(); }
2. Add a private field to add code to the test Controller:lifetimecontroller
private readonly Iuserservice _userservice1; private readonly Iuserservice _userservice2; private readonly Iarticleservice _articleservice1; private readonly Iarticleservice _articleservice2; private readonly Iproductservice _productservice1; private readonly Iproductservice _productservice2;
3. Adding a construction method
Public Lifetimecontroller ( iuserservice UserService1, Iuserservice userService2, iarticleservice ArticleService1, Iarticleservice articleService2, iproductservice ProductService1, Iproductservice ProductService2 ) { = UserService1; = UserService2; = ArticleService1; = ArticleService2; = ProductService1; = productService2; }
4. Add test code
PublicIactionresult Index () {varSB =NewStringBuilder (); Sb. Append ("transient1:"+ _userservice1.gethashcode () +"<br/>"); Sb. Append ("Transient2:"+ _userservice2.gethashcode () +"<br/>"); Sb. Append ("scope1:"+ _articleservice1.gethashcode () +"<br/>"); Sb. Append ("scope2:"+ _articleservice2.gethashcode () +"<br/>"); Sb. Append ("Singleton1:"+ _productservice1.gethashcode () +"<br/>"); Sb. Append ("Singleton2:"+ _productservice2.gethashcode () +"<br/>"); Response.ContentType="text/html"; returnContent (sb.) ToString ()); }
5. Implementation results
First time refresh:
transient1:66454027
transient2:35021870
scope1:38350037
scope2:38350037
singleton1:4417230
singleton2:4417230
Second refresh:
transient1:103653
transient2:5079042
scope1:47546512
scope2:47546512
singleton1:4417230
singleton2:4417230
Visible
The life cycle of the transient type is different for each use, and differs from one class to another using different methods.
The life cycle of the scope type is the same within the same request
Singleton type of life cycle, each request is the same
So understanding the role of life cycle, we can in the development of the different services to choose different life cycle.
The life cycle of the ASP. NET Core Service