dotnet/aspnetcore · error · InvalidOperationException
' {GetType().Name}' has not been initialized.
Error message
' {GetType().Name}' has not been initialized. What it means
AssertInitialized throws InvalidOperationException when, after calling EnsureInitialized, _isInitialized is still false. This means the derived NavigationManager never called Initialize, so BaseUri/Uri are not set and any operation requiring them is unsafe.
Source
Thrown at src/Components/Components/src/NavigationManager.cs:611
{
AssertInitialized();
if (_locationChangingHandlers.Remove(locationChangingHandler) && _locationChangingHandlers.Count == 0)
{
SetNavigationLockState(false);
}
}
private void AssertInitialized()
{
if (!_isInitialized)
{
EnsureInitialized();
}
if (!_isInitialized)
{
throw new InvalidOperationException($"'{GetType().Name}' has not been initialized.");
}
}
private static bool TryGetLengthOfBaseUriPrefix(Uri baseUri, string uri, out int length)
{
if (uri.StartsWith(baseUri.OriginalString, StringComparison.Ordinal))
{
// The absolute URI must be of the form "{baseUri}something" (where
// baseUri ends with a slash), and from that we return "something"
length = baseUri.OriginalString.Length;
return true;
}
var pathEndIndex = uri.AsSpan().IndexOfAny('#', '?');
var uriPathOnly = pathEndIndex < 0 ? uri : uri.AsSpan(0, pathEndIndex);
if (baseUri.OriginalString.EndsWith('/') && uriPathOnly.Equals(baseUri.OriginalString.AsSpan(0, baseUri.OriginalString.Length - 1), StringComparison.Ordinal))
{
// Special case: for the base URI "/something/", if you're atView on GitHub (pinned to 294cab2f9b)
Solutions
- In your NavigationManager subclass, override EnsureInitialized (or the constructor) and call Initialize(baseUri, uri) once.
- Ensure the host (renderer/server) initializes NavigationManager before rendering components that consume it.
- In tests, call Initialize on the stub before exercising navigation code.
Example fix
// before
public class TestNavManager : NavigationManager { /* Initialize never called */ }
// after
public class TestNavManager : NavigationManager
{
public TestNavManager() => Initialize("http://localhost/", "http://localhost/");
} Defensive patterns
Strategy: validation
Validate before calling
// Cannot read _isInitialized; ensure Initialize is called before use
public void EnsureReady()
{
// Trigger AssertInitialized's lazy path or call Initialize once:
try { _ = BaseUri; }
catch (InvalidOperationException) { Initialize(_defaultBase, _defaultUri); }
} Try / catch
try { var uri = navManager.Uri; }
catch (InvalidOperationException ex) when (ex.Message.Contains("not been initialized"))
{ logger.LogError(ex, "NavigationManager used before Initialize"); throw; } Prevention
- Always call Initialize in custom NavigationManager constructors or EnsureInitialized.
- Ensure the host sets up NavigationManager before rendering components.
- Initialize stub managers in tests before exercising navigation code.
When it happens
Trigger: Using a custom NavigationManager whose EnsureInitialized override does not call Initialize; accessing Uri/BaseUri/NavigateTo/LocationChanged before the host has initialized the manager; DI resolving NavigationManager too early in a non-Blazor host.
Common situations: Console/non-Blazor host wiring missing the Initialize call; component rendering attempted before the renderer set up NavigationManager; test harness that constructs NavigationManager but forgets Initialize.
Related errors
- ' {GetType().Name}' already initialized.
- Services cannot be accessed before the component is initiali
- PersistentComponentState already initialized.
- Renderer with ID '${rendererId}' is not listening for naviga
- No enhanced programmatic navigation handler has been attache
AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06).
Data as JSON: /api/errors/c576057688bd5444.
Report an issue: GitHub.