Life cycle of asp.net core service

Transient: Every time GetService creates a new instance

Scoped: Only one instance is initialized in the same Scope, which can be understood as (only one instance is created for each request level, and the same http request will be in one scope)

Singleton: Only one instance is created for the entire application life cycle 

 

The above description is relatively abstract and not easy to understand. It will be more intuitive to explain with examples.

The following demonstrates through specific examples.

Define three empty interfaces: IArticleService, IProductService, IUserService

Then define three implementations: ArticleService, ProductService, UserService

1. Inject the interface and implementation into the DI container

Add the following 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 and add the following code in 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. Add a constructor

        public LifeTimeController(
            IUserService userService1, IUserService userService2,
            IArticleService articleService1, IArticleService articleService2,
            IProductService productService1, IProductService productService2
        )
        {
            _userService1 = userService1;
            _userService2 = userService2;
            _articleService1 = articleService1;
            _articleService2 = articleService2;
            _productService1 = productService1;
            _productService2 = productService2;
        }

4. Add test code

        public IActionResult Index()
        {
            var sb = new StringBuilder ();
            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";
            return Content(sb.ToString());
        }

5. Execution results

First 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 transient type is different every time it is used, and different classes or different methods are used differently

The life cycle of the scope type is the same within the same request

The life cycle of singleton type is the same for every request

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325016680&siteId=291194637