{"record":{"id":"541b32ba36811741","repo":"lepoco/wpfui","slug":"the-dialoghost-was-never-set","errorCode":null,"errorMessage":"The DialogHost was never set.","messagePattern":"The DialogHost was never set\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/ContentDialogService.cs","lineNumber":130,"sourceCode":"    /// <inheritdoc/>\n    public ContentDialogHost? GetDialogHostEx()\n    {\n        return _dialogHostEx;\n    }\n\n    /// <inheritdoc/>\n    public Task<ContentDialogResult> ShowAsync(ContentDialog dialog, CancellationToken cancellationToken)\n    {\n#pragma warning disable CS0618 // (Warning: Obsolete) To maintain compatibility\n\n        if (dialog == null)\n        {\n            throw new ArgumentNullException(nameof(dialog));\n        }\n\n        if (_dialogHostEx == null && _dialogHost == null)\n        {\n            throw new InvalidOperationException(\"The DialogHost was never set.\");\n        }\n\n        object? svcHost = _dialogHostEx is not null ? _dialogHostEx : _dialogHost;\n\n        object? dlgHost = dialog.DialogHostEx is not null ? dialog.DialogHostEx : dialog.DialogHost;\n\n        if (dlgHost != null && !ReferenceEquals(dlgHost, svcHost))\n        {\n            throw new InvalidOperationException(\n                \"The DialogHost is not the same as the one that was previously set.\"\n            );\n        }\n\n        if (_dialogHostEx != null)\n        {\n            dialog.DialogHostEx = _dialogHostEx;\n        }\n        else","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/ContentDialogService.cs#L112-L148","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nawait _dialogService.ShowAsync(dialog, ct); // no host set\n\n// after\n_dialogService.SetDialogHost(RootContentDialogHost);\nawait _dialogService.ShowAsync(dialog, ct);","handlingStrategy":"validation","validationCode":"if (_dialogService.GetDialogHostEx() is null && _dialogService.GetDialogHost() is null) { /* set host before ShowAsync */ }","typeGuard":"static bool HostIsConfigured(IContentDialogService svc) => svc.GetDialogHostEx() is not null || svc.GetDialogHost() is not null;","tryCatchPattern":null,"preventionTips":["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."],"tags":["content-dialog","host","showasync","configuration"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}