What is the purpose of OwningComponentBase, and how does it help manage the lifetime of dependencies within a specific component?
Last updated
OwningComponentBasegives a component its own private DI scope (exposed asScopedServices), so scoped services like aDbContextare disposed with the component instead of living for the whole Blazor Server circuit.OwningComponentBase<T>adds a ready-resolvedServiceproperty of type T from that owned scope.Don't use
[Inject]for services you want component-scoped, because it resolves from the circuit's scope, not the owned one.
- https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-10.0
- https://chrissainty.com/an-introduction-to-owningcomponentbase/
- https://blazor-university.com/dependency-injection/component-scoped-dependencies/owningcomponentbase-generic/
- https://blazor-university.com/dependency-injection/component-scoped-dependencies/owning-multiple-dependencies-the-wrong-way/
- https://blazor-university.com/dependency-injection/component-scoped-dependencies/owning-multiple-dependencies-the-right-way/