MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException

{nameof(DialogHost)} does not have a current session

Error message

{nameof(DialogHost)} does not have a current session

What it means

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.

Source

Thrown at src/MaterialDesignThemes.Wpf/DialogHost.cs:803

        set => SetValue(DialogClosedCallbackProperty, value);
    }

    protected void OnDialogClosed(DialogClosedEventArgs eventArgs)
        => RaiseEvent(eventArgs);

    #endregion

    internal void AssertTargetableContent()
    {
        var existingBinding = BindingOperations.GetBindingExpression(this, DialogContentProperty);
        if (existingBinding != null)
            throw new InvalidOperationException(
                "Content cannot be passed to a dialog via the OpenDialog if DialogContent already has a binding.");
    }

    internal void InternalClose(object? parameter)
    {
        var currentSession = CurrentSession ?? throw new InvalidOperationException($"{nameof(DialogHost)} does not have a current session");

        currentSession.CloseParameter = parameter;
        currentSession.IsEnded = true;

        //multiple ways of calling back that the dialog is closing:
        // * routed event
        // * the attached property (which should be applied to the button which opened the dialog
        // * straight forward IsOpen dependency property 
        // * handler provided to the async show method
        var dialogClosingEventArgs = new DialogClosingEventArgs(currentSession, DialogClosingEvent);
        OnDialogClosing(dialogClosingEventArgs);
        _attachedDialogClosingEventHandler?.Invoke(this, dialogClosingEventArgs);
        DialogClosingCallback?.Invoke(this, dialogClosingEventArgs);
        _asyncShowClosingEventHandler?.Invoke(this, dialogClosingEventArgs);

        if (dialogClosingEventArgs.IsCancelled)
        {
            currentSession.IsEnded = false;

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Guard close calls with 'if (dialogHost.CurrentSession != null) ...' or check IsOpen before closing.
  2. Debounce/disable the close button after first click to prevent double-close.
  3. Ensure CloseDialogCommand is only reachable from inside an open dialog's visual tree.

Example fix

// before
dialogHost.InternalClose(parameter); // throws if not open

// after
if (dialogHost.IsOpen && dialogHost.CurrentSession is { } session)
    session.Close(parameter);
Defensive patterns

Strategy: validation

Validate before calling

if (dialogHost.CurrentSession is null)
    return; // nothing to close

dialogHost.CurrentSession.Close(parameter);

Type guard

static bool CanClose(DialogHost host) => host is { IsOpen: true, CurrentSession: not null };

Try / catch

try
{
    dialogHost.InternalClose(parameter);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("does not have a current session"))
{
    // dialog already dismissed; ignore
}

Prevention

When it happens

Trigger: Invoking DialogHost.CloseDialogCommand, session.Close(), or directly calling InternalClose when the dialog is already closed/never opened, so CurrentSession is null.

Common situations: Double-close (user clicks close button twice rapidly); close commands bound but invoked after dialog dismissed; programmatic close racing with the closing handler.

Related errors


AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13). Data as JSON: /api/errors/be1832620d22510e. Report an issue: GitHub.