{"record":{"id":"be1832620d22510e","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"nameof-dialoghost-does-not-have-a-current-sessi","errorCode":null,"errorMessage":"{nameof(DialogHost)} does not have a current session","messagePattern":"(.+?) does not have a current session","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/DialogHost.cs","lineNumber":803,"sourceCode":"        set => SetValue(DialogClosedCallbackProperty, value);\n    }\n\n    protected void OnDialogClosed(DialogClosedEventArgs eventArgs)\n        => RaiseEvent(eventArgs);\n\n    #endregion\n\n    internal void AssertTargetableContent()\n    {\n        var existingBinding = BindingOperations.GetBindingExpression(this, DialogContentProperty);\n        if (existingBinding != null)\n            throw new InvalidOperationException(\n                \"Content cannot be passed to a dialog via the OpenDialog if DialogContent already has a binding.\");\n    }\n\n    internal void InternalClose(object? parameter)\n    {\n        var currentSession = CurrentSession ?? throw new InvalidOperationException($\"{nameof(DialogHost)} does not have a current session\");\n\n        currentSession.CloseParameter = parameter;\n        currentSession.IsEnded = true;\n\n        //multiple ways of calling back that the dialog is closing:\n        // * routed event\n        // * the attached property (which should be applied to the button which opened the dialog\n        // * straight forward IsOpen dependency property \n        // * handler provided to the async show method\n        var dialogClosingEventArgs = new DialogClosingEventArgs(currentSession, DialogClosingEvent);\n        OnDialogClosing(dialogClosingEventArgs);\n        _attachedDialogClosingEventHandler?.Invoke(this, dialogClosingEventArgs);\n        DialogClosingCallback?.Invoke(this, dialogClosingEventArgs);\n        _asyncShowClosingEventHandler?.Invoke(this, dialogClosingEventArgs);\n\n        if (dialogClosingEventArgs.IsCancelled)\n        {\n            currentSession.IsEnded = false;","sourceCodeStart":785,"sourceCodeEnd":821,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/DialogHost.cs#L785-L821","documentation":"InternalClose throws InvalidOperationException when CurrentSession is null (DialogHost.cs:803). InternalClose is the host's close path driven by a DialogSession; with no live session the host has nothing to close. Typically triggered when code calls a close API on a host that is not currently showing a dialog.","triggerScenarios":"Invoking DialogHost.CloseDialogCommand, session.Close(), or directly calling InternalClose when the dialog is already closed/never opened, so CurrentSession is null.","commonSituations":"Double-close (user clicks close button twice rapidly); close commands bound but invoked after dialog dismissed; programmatic close racing with the closing handler.","solutions":["Guard close calls with 'if (dialogHost.CurrentSession != null) ...' or check IsOpen before closing.","Debounce/disable the close button after first click to prevent double-close.","Ensure CloseDialogCommand is only reachable from inside an open dialog's visual tree."],"exampleFix":"// before\ndialogHost.InternalClose(parameter); // throws if not open\n\n// after\nif (dialogHost.IsOpen && dialogHost.CurrentSession is { } session)\n    session.Close(parameter);","handlingStrategy":"validation","validationCode":"if (dialogHost.CurrentSession is null)\n    return; // nothing to close\n\ndialogHost.CurrentSession.Close(parameter);","typeGuard":"static bool CanClose(DialogHost host) => host is { IsOpen: true, CurrentSession: not null };","tryCatchPattern":"try\n{\n    dialogHost.InternalClose(parameter);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not have a current session\"))\n{\n    // dialog already dismissed; ignore\n}","preventionTips":["Check dialogHost.IsOpen and CurrentSession before closing.","Debounce/disable close buttons to avoid double-close.","Prefer session.Close over direct InternalClose so IsEnded guards apply."],"tags":["wpf","materialdesigninxaml","dialog","state","lifecycle"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}