{"record":{"id":"232ef111ce663623","repo":"lepoco/wpfui","slug":"cannot-set-contentpresenter-a-contentdialoghost-h","errorCode":null,"errorMessage":"Cannot set ContentPresenter: a ContentDialogHost host has already been set. Only one host type is allowed per instance for compatibility.","messagePattern":"Cannot set ContentPresenter: a ContentDialogHost 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":61,"sourceCode":"\n    [Obsolete(\"Use GetDialogHost instead.\")]\n    public ContentPresenter? GetContentPresenter()\n    {\n        return GetDialogHost();\n    }\n\n    /// <inheritdoc/>\n    [Obsolete(\"Use SetDialogHost(ContentDialogHost) instead.\")]\n    public void SetDialogHost(ContentPresenter contentPresenter)\n    {\n        if (contentPresenter == null)\n        {\n            throw new ArgumentNullException(nameof(contentPresenter));\n        }\n\n        if (_dialogHostEx != null)\n        {\n            throw new InvalidOperationException(\n                \"Cannot set ContentPresenter: a ContentDialogHost host has already been set. \"\n                    + \"Only one host type is allowed per instance for compatibility.\"\n            );\n        }\n\n        _dialogHost = contentPresenter;\n    }\n\n    /// <inheritdoc/>\n    [Obsolete(\"Use GetDialogHostEx() instead.\")]\n    public ContentPresenter? GetDialogHost()\n    {\n        return _dialogHost;\n    }\n\n    /// <inheritdoc/>\n    /// <exception cref=\"ArgumentNullException\">\n    /// Thrown when <paramref name=\"dialogHost\"/> is <see langword=\"null\"/>.","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/ContentDialogService.cs#L43-L79","documentation":"Thrown by ContentDialogService.SetDialogHost(ContentPresenter) when the new-style host (_dialogHostEx of type ContentDialogHost) has already been set. The service forbids mixing legacy ContentPresenter hosting with the new ContentDialogHost to keep rendering/behavior consistent within one instance.","triggerScenarios":"Calling the obsolete SetDialogHost(ContentPresenter) after SetDialogHost(ContentDialogHost) was already called on the same service instance. The field _dialogHostEx is non-null, triggering the InvalidOperationException.","commonSituations":"Migrating an app to the new ContentDialogHost API while leftover XAML/code still calls the legacy SetDialogHost(ContentPresenter). Or DI wiring that registers both host setups for the same singleton service.","solutions":["Pick one hosting model per service instance: use SetDialogHost(ContentDialogHost) and remove all legacy SetDialogHost(ContentPresenter) calls.","Search the codebase for the obsolete SetDialogHost(ContentPresenter) usages (CS0618 warnings) and migrate them.","If both are genuinely needed, use two separate ContentDialogService instances rather than mixing hosts on one."],"exampleFix":"// before\ncontentDialogService.SetDialogHost(myContentPresenter); // _dialogHostEx already set\n\n// after\ncontentDialogService.SetDialogHost(myContentDialogHost);","handlingStrategy":"validation","validationCode":"if (_dialogService.GetDialogHostEx() is null) { _dialogService.SetDialogHost(contentPresenter); }","typeGuard":"static bool HostIsUnset(IContentDialogService svc) => svc.GetDialogHost() is null && svc.GetDialogHostEx() is null;","tryCatchPattern":null,"preventionTips":["Adopt ContentDialogHost exclusively and remove obsolete SetDialogHost(ContentPresenter) calls.","Resolve CS0618 warnings to catch lingering legacy host usage.","Use one service instance per host type."],"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"}