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;
        }
        else

View on GitHub (pinned to ffebacd610)

Solutions

  1. Call SetDialogHost(ContentDialogHost) (or the legacy ContentPresenter) during the host Window's initialization, before any ShowAsync call.
  2. Verify the ContentDialogHost control exists in the hosting view's XAML and is passed to the service.
  3. 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

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


AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13). Data as JSON: /api/errors/541b32ba36811741. Report an issue: GitHub.