lepoco/wpfui · error · InvalidOperationException
The DialogHost was never set.
Error message
The DialogHost was never set.
What it means
Thrown by ContentDialogService.ShowAsync when neither host field is set (_dialogHostEx and _dialogHost are both null). The dialog needs a host surface to render into, so calling ShowAsync before SetDialogHost is a usage error.
Source
Thrown at src/Wpf.Ui/ContentDialogService.cs:130
/// <inheritdoc/>
public ContentDialogHost? GetDialogHostEx()
{
return _dialogHostEx;
}
/// <inheritdoc/>
public Task<ContentDialogResult> ShowAsync(ContentDialog dialog, CancellationToken cancellationToken)
{
#pragma warning disable CS0618 // (Warning: Obsolete) To maintain compatibility
if (dialog == null)
{
throw new ArgumentNullException(nameof(dialog));
}
if (_dialogHostEx == null && _dialogHost == null)
{
throw new InvalidOperationException("The DialogHost was never set.");
}
object? svcHost = _dialogHostEx is not null ? _dialogHostEx : _dialogHost;
object? dlgHost = dialog.DialogHostEx is not null ? dialog.DialogHostEx : dialog.DialogHost;
if (dlgHost != null && !ReferenceEquals(dlgHost, svcHost))
{
throw new InvalidOperationException(
"The DialogHost is not the same as the one that was previously set."
);
}
if (_dialogHostEx != null)
{
dialog.DialogHostEx = _dialogHostEx;
}
elseView on GitHub (pinned to ffebacd610)
Solutions
- Call SetDialogHost(ContentDialogHost) (or the legacy ContentPresenter) during the host Window's initialization, before any ShowAsync call.
- Verify the ContentDialogHost control exists in the hosting view's XAML and is passed to the service.
- If using DI, register the host setup as part of the window's Loaded handler so it always precedes dialog usage.
Example fix
// before await _dialogService.ShowAsync(dialog, ct); // no host set // after _dialogService.SetDialogHost(RootContentDialogHost); await _dialogService.ShowAsync(dialog, ct);
Defensive patterns
Strategy: validation
Validate before calling
if (_dialogService.GetDialogHostEx() is null && _dialogService.GetDialogHost() is null) { /* set host before ShowAsync */ } Type guard
static bool HostIsConfigured(IContentDialogService svc) => svc.GetDialogHostEx() is not null || svc.GetDialogHost() is not null;
Prevention
- Always call SetDialogHost before the first ShowAsync.
- Register host setup in the host Window's Loaded handler.
- Verify the ContentDialogHost control exists in the hosting view.
When it happens
Trigger: Invoking ShowAsync on a ContentDialogService instance without ever calling SetDialogHost(ContentDialogHost) or SetDialogHost(ContentPresenter). Common when the service is resolved from DI but the host was never wired to a view.
Common situations: Forgetting to call SetDialogHost in the host Window/Page initialization, a DI registration that creates the service but does not bind it to a ContentDialogHost control, or showing a dialog before the host view has set the presenter.
Related errors
- The DialogHost is not the same as the one that was previousl
- Cannot change DialogHost while the dialog is being shown.
- Cannot set ContentPresenter: a ContentDialogHost host has al
- Cannot set ContentDialogHost: a legacy ContentPresenter host
- Cannot set DialogHost when DialogHostEx is already set.
AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13).
Data as JSON: /api/errors/541b32ba36811741.
Report an issue: GitHub.