dotnet/aspnetcore · error · InvalidOperationException
Services cannot be accessed before the component is initiali
Error message
Services cannot be accessed before the component is initialized.
What it means
OwningComponentBase.ScopedServices throws InvalidOperationException when the injected IServiceScopeFactory (ScopeFactory) is still null, i.e. before Blazor has performed property injection on the component. Accessing ScopedServices (or Service on the generic variant) earlier than OnInitialized is therefore invalid.
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 294cab2f9b)
Solutions
- Defer ScopedServices access to OnInitialized/OnInitializedAsync or later lifecycle methods.
- Ensure the component is created by the Blazor renderer (which performs injection), not via new().
- In tests, set the ScopeFactory property manually before accessing ScopedServices.
Example fix
// before
public class MyComp : OwningComponentBase
{
private readonly Repo _repo = ScopedServices.GetRequiredService<Repo>(); // runs in ctor -> throws
}
// after
public class MyComp : OwningComponentBase
{
private Repo _repo = default!;
protected override void OnInitialized() => _repo = ScopedServices.GetRequiredService<Repo>();
} Defensive patterns
Strategy: validation
Validate before calling
protected override void OnInitialized()
{
// Safe to access ScopedServices here; injection has completed
_repo = ScopedServices.GetRequiredService<Repo>();
} Try / catch
try { var svc = ScopedServices; }
catch (InvalidOperationException ex) when (ex.Message.Contains("before the component is initialized"))
{ logger.LogError(ex, "ScopedServices accessed too early"); throw; } Prevention
- Only touch ScopedServices from OnInitialized onward.
- Never new up OwningComponentBase-derived components manually.
- In tests, set the ScopeFactory property before access.
When it happens
Trigger: Reading ScopedServices from a field initializer or constructor of an OwningComponentBase-derived component; accessing ScopedServices before the renderer injects [Inject] properties; using the component outside the Blazor renderer (manual instantiation).
Common situations: Constructor logic that touches ScopedServices; base-class field initializers running before injection; tests that new up the component directly.
Related errors
- ' {GetType().Name}' already initialized.
- ' {GetType().Name}' has not been initialized.
- PersistentComponentState already initialized.
- Worker .NET runtime not loaded
- Dynamic root components have not been enabled in this applic
AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06).
Data as JSON: /api/errors/29d3a893ce8d84b4.
Report an issue: GitHub.