如下是我在stackoverflow拷贝的一句话,觉得正确。
最难理解的就是这个Scope:,我们大部分情况下,一个Scoped就是一个http request请求。
其实上Scope就是Scope
container.CreateScope().ServiceProvider.GetService<ITestService>();这里就是新建立一个Scope了。这app.net core console里可以这样用。在asp.net core mvc里,每个request会默认创建了一个scope.
---stackoverflow---
This is the single most complicated explanation of a transient service I've ever seen. Transient = Any time this service is resolved is the equivalent of assigning your variable new TService
. Scoped will cache the first initialisation of it for that "scope" (http request in most cases). Singleton will cache only one instance ever for the lifetime of the application, Simple as that. The above diagrams are so convoluted.