{"record":{"id":"18529a628b3d55aa","repo":"lepoco/wpfui","slug":"dialoghost-was-not-set","errorCode":null,"errorMessage":"DialogHost was not set","messagePattern":"DialogHost was not set","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/ContentDialog/ContentDialog.cs","lineNumber":718,"sourceCode":"\n    private void UpdateIsLegacyHost()\n    {\n        SetValue(IsLegacyHostPropertyKey, _dialogHostEx is null);\n    }\n\n    /// <summary>\n    /// Shows the dialog\n    /// </summary>\n    [System.Diagnostics.CodeAnalysis.SuppressMessage(\n        \"WpfAnalyzers.DependencyProperty\",\n        \"WPF0041:Set mutable dependency properties using SetCurrentValue\",\n        Justification = \"SetCurrentValue(ContentProperty, ...) will not work\"\n    )]\n    public async Task<ContentDialogResult> ShowAsync(CancellationToken cancellationToken = default)\n    {\n        if (_dialogHost is null && _dialogHostEx is null)\n        {\n            throw new InvalidOperationException(\"DialogHost was not set\");\n        }\n\n        // Uses `RunContinuationsAsynchronously` to execute continuations asynchronously\n        // rather than synchronously on the caller's stack when TCS completes.\n        //\n        // Benefits:\n        // - Prevents UI-thread reentrancy\n        // - Eliminates deadlock risks\n        // - Ensures predictable continuation scheduling\n        Tcs = new TaskCompletionSource<ContentDialogResult>(\n            TaskCreationOptions.RunContinuationsAsynchronously\n        );\n\n        CancellationTokenRegistration tokenRegistration = cancellationToken.Register(\n            o => Tcs.TrySetCanceled((CancellationToken)o!),\n            cancellationToken\n        );\n","sourceCodeStart":700,"sourceCodeEnd":736,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/ContentDialog/ContentDialog.cs#L700-L736","documentation":"Thrown by ContentDialog.ShowAsync when both the modern DialogHost and the legacy DialogHostEx backing fields are null. The dialog has nowhere to inject its content, so it refuses to show. This is a configuration precondition failure — the host must be supplied before the async show call.","triggerScenarios":"Calling await dialog.ShowAsync() on a ContentDialog whose DialogHost (or DialogHostEx) was never set — e.g. the dialog was created in code-behind without a host, or placed in a tree that has no ContentDialogHost ancestor, or the host was nulled out before showing.","commonSituations":"Building a ContentDialog in code-behind and forgetting to assign DialogHost; using a ContentDialog inside a plain Window (not a FluentWindow with a ContentDialogHost); host set via XAML but the dialog is shown before Loaded so the host resolution has not run.","solutions":["Set dialog.DialogHost (or DialogHostEx) to a ContentDialogHost instance before calling ShowAsync.","Use FluentWindow which provides a default ContentDialogHost, and show the dialog after the window has loaded.","In XAML, ensure a <ui:ContentDialogHost> exists in the window's content and the dialog is shown from a context where the host is reachable.","Verify the host is non-null in the debugger immediately before the ShowAsync call."],"exampleFix":"// before\nvar dialog = new ContentDialog { Content = \"hi\" };\nawait dialog.ShowAsync(); // throws\n\n// after\nvar dialog = new ContentDialog { Content = \"hi\", DialogHost = host };\nawait dialog.ShowAsync();","handlingStrategy":"validation","validationCode":"if (dialog.DialogHost is null && dialog.DialogHostEx is null)\n    throw new InvalidOperationException(\"Configure DialogHost before ShowAsync.\");\n\nawait dialog.ShowAsync();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set DialogHost at construction or in XAML before the first ShowAsync.","Show dialogs from a FluentWindow that provides a default host.","Assert the host is non-null in DEBUG right before ShowAsync."],"tags":["wpf","contentdialog","configuration","precondition"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}