简体   繁体   中英

Application Lifetime and Add Singleton DI service in ASP.NET Core

I am trying to understand the singleton lifetime in the context of DI in ASP.NET Core.

First, from what I can see a Singleton instance will be available "across application lifetime." But what does "application lifetime" mean? If it's a website and if a user logs in, singleton created first and user logs out and comes back in to the site after a few hours and logs back in will that user gets the same singleton instance? what about multiple users? Can multiple users get the same singleton instance?

Second, When does application lifetime end in .NET Core? Most articles talk about registering for application shutdown events. But what triggers an application shutdown? Is it always external events like IISreset or a system shutdown? Or internal events like memory crash etc? If no resets or memory or any other issues, will it keep going forever?

Singleton here (ie not the singleton design pattern) means a single instance for the created root IServiceProvider , so effectively a single instance will be used for all requests, until the process exits.

It has nothing to do with per request or session. If you want a new instance per request, but shared, use the scoped lifetime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM