dotnet/aspnetcore · error · InvalidOperationException
Services cannot be accessed before the component is…
Error message
Services cannot be accessed before the component is initialized.
What it means
Thrown by the ScopedServices getter on OwningComponentBase when ScopeFactory (the [Inject]ed IServiceScopeFactory) is still null. OwningComponentBase creates a DI scope on demand, but the factory is only assigned by the components framework during initialization; accessing ScopedServices before that point is invalid.
Solutions
- Move service resolution into OnInitialized, OnInitializedAsync, or later lifecycle methods.
- Do not read ScopedServices/Service from constructors or field initializers.
- If you must hold a reference, lazily resolve it on first use inside a lifecycle method and cache the result in a field.
- Ensure the component is actually rendered by the Blazor renderer (not new'd up manually), so injection runs.
Example fix
// before: resolve in constructor - ScopeFactory is null
public class MyComponent : OwningComponentBase
{
private readonly IRepo _repo;
public MyComponent() => _repo = ScopedServices.GetRequiredService<IRepo>();
}
// after: resolve in a lifecycle hook
public class MyComponent : OwningComponentBase
{
private IRepo _repo = default!;
protected override void OnInitialized() => _repo = ScopedServices.GetRequiredService<IRepo>();
} Defensive patterns
Strategy: validation
Validate before calling
if (ScopeFactory is null) throw new InvalidOperationException("Component not initialized; access ScopedServices from OnInitialized."); Try / catch
try { service = ScopedServices.GetRequiredService<T>(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("component is initialized"))
{ /* defer resolution to OnInitialized */ } Prevention
- Only read ScopedServices/Service from OnInitializedAsync or later lifecycle hooks.
- Do not resolve services in constructors or field initializers of components.
- Cache resolved services in fields set during OnInitialized so later code reads fields, not ScopedServices.
When it happens
Trigger: Reading ScopedServices (or the derived Service property of OwningComponentBase<TService>) from a constructor, a field initializer, or any code that runs before SetParametersAsync/OnInitialized. The [Inject] attributes have not yet been populated at that point, so ScopeFactory is null.
Common situations: Constructor of an OwningComponentBase-derived component that resolves a service eagerly; a property initializer that calls ScopedServices; accessing Service in OnParametersSet when the component was never initialized (e.g. used outside the renderer).
Related errors
- The provider was not found in the providers list of the…
- The subscriber with the given section ID
- There are no content providers with the given section ID
- ' ' is flagged with SingleDelivery, but the selected…
- Authorization requires a cascading parameter of type Task
AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11).
Data as JSON: /api/errors/29d3a893ce8d84b4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Components/Components/src/OwningComponentBase.cs:37
private AsyncServiceScope? _scope;
[Inject] IServiceScopeFactory ScopeFactory { get; set; } = default!;
/// <summary>
/// Gets a value determining if the component and associated services have been disposed.
/// </summary>
protected bool IsDisposed { get; private set; }
/// <summary>
/// Gets the scoped <see cref="IServiceProvider"/> that is associated with this component.
/// </summary>
protected IServiceProvider ScopedServices
{
get
{
if (ScopeFactory == null)
{
throw new InvalidOperationException("Services cannot be accessed before the component is initialized.");
}
ObjectDisposedException.ThrowIf(IsDisposed, this);
_scope ??= ScopeFactory.CreateAsyncScope();
return _scope.Value.ServiceProvider;
}
}
/// <inheritdoc />
void IDisposable.Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases the service scope used by the component.View on GitHub (pinned to 3600ca084e)