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

  1. Move service resolution into OnInitialized, OnInitializedAsync, or later lifecycle methods.
  2. Do not read ScopedServices/Service from constructors or field initializers.
  3. If you must hold a reference, lazily resolve it on first use inside a lifecycle method and cache the result in a field.
  4. 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

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


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)