diff --git a/aspnetcore/fundamentals/dependency-injection.md b/aspnetcore/fundamentals/dependency-injection.md index f222cab4db..773421d69b 100644 --- a/aspnetcore/fundamentals/dependency-injection.md +++ b/aspnetcore/fundamentals/dependency-injection.md @@ -5,7 +5,7 @@ description: Learn how ASP.NET Core implements dependency injection and how to u monikerRange: '>= aspnetcore-2.1' ms.author: riande ms.custom: mvc -ms.date: 05/14/2020 +ms.date: 06/21/2020 no-loc: [Blazor, "Identity", "Let's Encrypt", Razor, SignalR] uid: fundamentals/dependency-injection --- @@ -186,10 +186,14 @@ Choose an appropriate lifetime for each registered service. ASP.NET Core service Transient lifetime services () are created each time they're requested from the service container. This lifetime works best for lightweight, stateless services. +In apps that process requests, transient services are disposed at the end of the request. + ### Scoped Scoped lifetime services () are created once per client request (connection). +In apps that process requests, scoped services are disposed at the end of the request. + > [!WARNING] > When using a scoped service in a middleware, inject the service into the `Invoke` or `InvokeAsync` method. Don't inject via [constructor injection](xref:mvc/controllers/dependency-injection#constructor-injection) because it forces the service to behave like a singleton. For more information, see . @@ -197,6 +201,8 @@ Scoped lifetime services () are created the first time they're requested (or when `Startup.ConfigureServices` is run and an instance is specified with the service registration). Every subsequent request uses the same instance. If the app requires singleton behavior, allowing the service container to manage the service's lifetime is recommended. Don't implement the singleton design pattern and provide user code to manage the object's lifetime in the class. +In apps that process requests, singleton services are disposed when the is disposed at app shutdown. + > [!WARNING] > It's dangerous to resolve a scoped service from a singleton. It may cause the service to have incorrect state when processing subsequent requests. @@ -763,10 +769,14 @@ Choose an appropriate lifetime for each registered service. ASP.NET Core service Transient lifetime services () are created each time they're requested from the service container. This lifetime works best for lightweight, stateless services. +In apps that process requests, transient services are disposed at the end of the request. + ### Scoped Scoped lifetime services () are created once per client request (connection). +In apps that process requests, scoped services are disposed at the end of the request. + > [!WARNING] > When using a scoped service in a middleware, inject the service into the `Invoke` or `InvokeAsync` method. Don't inject via [constructor injection](xref:mvc/controllers/dependency-injection#constructor-injection) because it forces the service to behave like a singleton. For more information, see . @@ -774,6 +784,8 @@ Scoped lifetime services () are created the first time they're requested (or when `Startup.ConfigureServices` is run and an instance is specified with the service registration). Every subsequent request uses the same instance. If the app requires singleton behavior, allowing the service container to manage the service's lifetime is recommended. Don't implement the singleton design pattern and provide user code to manage the object's lifetime in the class. +In apps that process requests, singleton services are disposed when the is disposed at app shutdown. + > [!WARNING] > It's dangerous to resolve a scoped service from a singleton. It may cause the service to have incorrect state when processing subsequent requests.