{"record":{"id":"2e5f9ff6dd4228aa","repo":"lepoco/wpfui","slug":"the-dialoghost-is-not-the-same-as-the-one-that-was","errorCode":null,"errorMessage":"The DialogHost is not the same as the one that was previously set.","messagePattern":"The DialogHost is not the same as the one that was previously set\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/ContentDialogService.cs","lineNumber":139,"sourceCode":"#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\n        {\n            dialog.DialogHost = _dialogHost;\n        }\n\n        return dialog.ShowAsync(cancellationToken);\n\n#pragma warning restore CS0618 // (Warning: Obsolete)\n    }\n}","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/ContentDialogService.cs#L121-L157","documentation":"Thrown by ShowAsync when the dialog carries its own host reference (dialog.DialogHostEx or dialog.DialogHost) that does not match the host registered on the service (ReferenceEquals fails). The service refuses to reparent a dialog whose host was pre-bound elsewhere, to prevent inconsistent rendering.","triggerScenarios":"A ContentDialog had its DialogHost/DialogHostEx explicitly set to a different host than the one the service holds, then ShowAsync is called. The mismatch check compares references and throws.","commonSituations":"Sharing a ContentDialog across windows/hosts, reusing a dialog instance after moving it to a different host, or explicitly setting DialogHost on the dialog in XAML while the service has its own host.","solutions":["Do not pre-set DialogHost/DialogHostEx on the ContentDialog; let the service assign it during ShowAsync.","If the dialog must carry a host, ensure it is the exact same instance the service was configured with.","Create a fresh ContentDialog per host context rather than reusing one across hosts."],"exampleFix":"// before\ndialog.DialogHostEx = someOtherHost;\nawait _dialogService.ShowAsync(dialog, ct); // mismatch\n\n// after\n// leave dialog.DialogHostEx unset; the service assigns its own host\nawait _dialogService.ShowAsync(dialog, ct);","handlingStrategy":"validation","validationCode":"if (dialog.DialogHostEx is not null && !ReferenceEquals(dialog.DialogHostEx, service.GetDialogHostEx())) { /* clear or realign before ShowAsync */ }","typeGuard":"static bool DialogHostMatches(ContentDialog d, object svcHost) => d.DialogHostEx is null || ReferenceEquals(d.DialogHostEx, svcHost);","tryCatchPattern":null,"preventionTips":["Do not pre-set DialogHost/DialogHostEx on the dialog; let the service assign it.","Use a fresh ContentDialog per host context.","Ensure the dialog's host reference equals the service's host when set explicitly."],"tags":["content-dialog","host","showasync","reference-mismatch"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}