liveBook · Manning

ScopedServices property

As explained in Pro ASP.NET Core 7, Tenth Edition

The ScopedServices property is an inherited property that exposes an IServiceProvider for services created in a component-specific dependency-injection scope.

ScopedServices is an inherited property from OwningComponentBase that exposes an IServiceProvider for services created in a component-specific dependency-injection scope. That scope follows the component lifecycle and is not shared with other components. section 36.3.1

Use it when a component needs isolated scoped services:

  • Inherit from OwningComponentBase. section 36.3.1
  • Obtain each service requiring the dedicated scope through ScopedServices; services injected with Inject remain in the request scope. section 36.3.1
  • Use OwningComponentBase<T> when one typed scoped service is sufficient; access it through Service. section 36.3.1

Obtaining a component-owned DataContext

A component can inherit OwningComponentBase and retrieve DataContext through ScopedServices: DataContext? Context => ScopedServices.GetService<DataContext>();. The typed alternative inherits OwningComponentBase<DataContext> and assigns Context => Service. section 36.3.1

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains inheritance, IServiceProvider retrieval, Inject behavior, and the typed alternative, but not provider internals.
Examples
Demonstrates retrieving DataContext through ScopedServices and through the typed Service property in an Editor.razor component.
What it assumes
Assumes familiarity with Blazor components and dependency injection while introducing component-owned scopes and IServiceProvider retrieval.
What you can do after
Operate/configure a Blazor component to resolve services from a lifecycle-specific dependency-injection scope.
How it connects
Connects dependency injection scopes, IServiceProvider, OwningComponentBase, OwningComponentBase<T>, Inject, and Entity Framework Core.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.