diff --git a/aspnetcore/blazor/fundamentals/dependency-injection.md b/aspnetcore/blazor/fundamentals/dependency-injection.md index c9f6263f32b9..1566d5b0b69c 100644 --- a/aspnetcore/blazor/fundamentals/dependency-injection.md +++ b/aspnetcore/blazor/fundamentals/dependency-injection.md @@ -179,9 +179,13 @@ Prerequisites for constructor injection: In ASP.NET Core apps, scoped services are typically scoped to the current request. After the request completes, any scoped or transient services are disposed by the DI system. In Blazor Server apps, the request scope lasts for the duration of the client connection, which can result in transient and scoped services living much longer than expected. In Blazor WebAssembly apps, services registered with a scoped lifetime are treated as singletons, so they live longer than scoped services in typical ASP.NET Core apps. + + An approach that limits a service lifetime in Blazor apps is use of the type. is an abstract type derived from that creates a DI scope corresponding to the lifetime of the component. Using this scope, it's possible to use DI services with a scoped lifetime and have them live as long as the component. When the component is destroyed, services from the component's scoped service provider are disposed as well. This can be useful for services that: * Should be reused within a component, as the transient lifetime is inappropriate. @@ -216,9 +220,11 @@ Two versions of the t For more information, see . + + ## Additional resources * @@ -559,7 +575,7 @@ For more information, see . ## Detect transient disposables -The following examples show how to detect disposable transient services in an app that should use . For more information, see the [Utility base component classes to manage a DI scope](#utility-base-component-classes-to-manage-a-di-scope) section. +The following example shows how to detect disposable transient services in an app that should use . For more information, see the [Utility base component classes to manage a DI scope](#utility-base-component-classes-to-manage-a-di-scope) section. ::: zone pivot="webassembly" @@ -664,16 +680,16 @@ public class TransientDependency The app can register transient disposables without throwing an exception. However, attempting to resolve a transient disposable results in an , as the following example shows. -`Pages/TransientDisposable.razor`: +`Pages/TransientExample.razor`: ```razor -@page "/transient-disposable" +@page "/transient-example" @inject TransientDisposable TransientDisposable

Transient Disposable Detection

``` -Navigate to the `TransientDisposable` component at `/transient-disposable` and an is thrown when the framework attempts to construct an instance of `TransientDisposable`: +Navigate to the `TransientExample` component at `/transient-example` and an is thrown when the framework attempts to construct an instance of `TransientDisposable`: > System.InvalidOperationException: Trying to resolve transient disposable service TransientDisposable in the wrong scope. Use an 'OwningComponentBase\' component base class for the service 'T' you are trying to resolve. @@ -913,7 +929,7 @@ For more information, see . ## Detect transient disposables -The following examples show how to detect disposable transient services in an app that should use . For more information, see the [Utility base component classes to manage a DI scope](#utility-base-component-classes-to-manage-a-di-scope) section. +The following example shows how to detect disposable transient services in an app that should use . For more information, see the [Utility base component classes to manage a DI scope](#utility-base-component-classes-to-manage-a-di-scope) section. ::: zone pivot="webassembly" @@ -1018,16 +1034,16 @@ public class TransientDependency The app can register transient disposables without throwing an exception. However, attempting to resolve a transient disposable results in an , as the following example shows. -`Pages/TransientDisposable.razor`: +`Pages/TransientExample.razor`: ```razor -@page "/transient-disposable" +@page "/transient-example" @inject TransientDisposable TransientDisposable

Transient Disposable Detection

``` -Navigate to the `TransientDisposable` component at `/transient-disposable` and an is thrown when the framework attempts to construct an instance of `TransientDisposable`: +Navigate to the `TransientExample` component at `/transient-example` and an is thrown when the framework attempts to construct an instance of `TransientDisposable`: > System.InvalidOperationException: Trying to resolve transient disposable service TransientDisposable in the wrong scope. Use an 'OwningComponentBase\' component base class for the service 'T' you are trying to resolve.