{"record":{"id":"b3d9e2211649ec9d","repo":"lepoco/wpfui","slug":"cannot-set-contentdialoghost-a-legacy-contentpres","errorCode":null,"errorMessage":"Cannot set ContentDialogHost: a legacy ContentPresenter host has already been set. Only one host type is allowed per instance for compatibility.","messagePattern":"Cannot set ContentDialogHost: a legacy ContentPresenter host has already been set\\. Only one host type is allowed per instance for compatibility\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/ContentDialogService.cs","lineNumber":103,"sourceCode":"    /// <remarks>\n    /// <para>\n    /// This method sets the enhanced <see cref=\"ContentDialogHost\"/> to contain and manage dialogs.\n    /// For compatibility reasons, an instance can have either a legacy host (set via\n    /// <see cref=\"SetDialogHost(ContentPresenter)\"/>) or an enhanced host (set via this method),\n    /// but not both.\n    /// </para>\n    /// </remarks>\n    public void SetDialogHost(ContentDialogHost dialogHost)\n    {\n        if (dialogHost == null)\n        {\n            throw new ArgumentNullException(nameof(dialogHost));\n        }\n\n        // Defense mechanism: prevent mixed host types for compatibility\n        if (_dialogHost != null)\n        {\n            throw new InvalidOperationException(\n                \"Cannot set ContentDialogHost: a legacy ContentPresenter host has already been set. \"\n                    + \"Only one host type is allowed per instance for compatibility.\"\n            );\n        }\n\n        _dialogHostEx = dialogHost;\n    }\n\n    /// <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","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/ContentDialogService.cs#L85-L121","documentation":"Thrown by ContentDialogService.SetDialogHost(ContentDialogHost) when the legacy host (_dialogHost of type ContentPresenter) is already set. This is the symmetric guard to error 11: once a ContentPresenter host is in use, switching to ContentDialogHost on the same instance is rejected to avoid mixed hosting.","triggerScenarios":"Calling SetDialogHost(ContentDialogHost) after the obsolete SetDialogHost(ContentPresenter) was already invoked. The field _dialogHost is non-null, so the InvalidOperationException fires.","commonSituations":"App initializes with the legacy ContentPresenter host (from older tutorials) and then attempts to upgrade to ContentDialogHost without clearing the prior host, or two different code paths each set a host type.","solutions":["Standardize on ContentDialogHost for the instance and avoid the legacy ContentPresenter host entirely.","If mid-migration, ensure only one SetDialogHost call runs per service instance; guard with a flag or restructure DI registration.","Use distinct ContentDialogService instances when both host styles must coexist in the app."],"exampleFix":"// before\ncontentDialogService.SetDialogHost(myContentDialogHost); // _dialogHost already set\n\n// after\n// remove the earlier legacy call and use only:\ncontentDialogService.SetDialogHost(myContentDialogHost);","handlingStrategy":"validation","validationCode":"if (_dialogService.GetDialogHost() is null) { _dialogService.SetDialogHost(dialogHost); }","typeGuard":"static bool LegacyHostUnset(IContentDialogService svc) => svc.GetDialogHost() is null;","tryCatchPattern":null,"preventionTips":["Avoid mixing legacy and new hosts on a single service instance.","Centralize host setup in one initialization path.","Migrate all legacy ContentPresenter host code to ContentDialogHost."],"tags":["content-dialog","host","api-migration","obsolete"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}