dotnet/wpf · error · InvalidOperationException
InvalidOperationException(SR.OperationCannotBePerformed, e)
Error message
InvalidOperationException(SR.OperationCannotBePerformed, e)
What it means
CallDoDefaultAction wraps its non-critical exceptions and rethrows them as InvalidOperationException(SR.OperationCannotBePerformed, e). When the underlying Win32/MSAA default-action call fails with any non-critical error (COM failure, lost element, etc.), the proxy converts it into this generic operation-failed exception while preserving the inner exception.
Solutions
- Inspect the InnerException to find the real root cause (COM hr, ElementNotAvailable, etc.)
- Re-acquire the element and retry — stale references are the most common cause
- Verify the control actually supports the default action via its accDoDefaultAction/defaultAction property
- Catch InvalidOperationException around pattern calls and treat it as 'action not performed' in test logic
Example fix
// before
try { invokePattern.Invoke(); }
catch (InvalidOperationException) { /* cause lost */ }
// after
try
{
invokePattern.Invoke();
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"Action failed: {ex.InnerException?.Message ?? ex.Message}");
element = FindElementAgain(); // re-acquire stale reference
} Defensive patterns
Strategy: try-catch
Validate before calling
// Re-verify the element exists and is enabled before invoking: if (element == null || !(bool)element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty)) return;
Type guard
bool IsInvokable(AutomationElement e) => e?.GetCurrentPattern(InvokePattern.Pattern) is InvokePattern;
Try / catch
try { invokePattern.Invoke(); } catch (InvalidOperationException ex) { Log(ex.InnerException); ReacquireAndRetry(); } Prevention
- Always log InnerException — it carries the real native failure
- Re-acquire elements after any UI change instead of reusing stale references
- Verify defaultAction support on the control before invoking
- Keep automation actions serialized; avoid racing the target app's own UI updates
When it happens
Trigger: Calling Invoke()/Toggle() on an MSAA proxy where the underlying DoDefaultAction (e.g. IAccessible::accDoDefaultAction or SendMessage) throws a non-critical exception — element vanished mid-call, COM error, or the action is not actually supported by the control.
Common situations: Target control disappears between the IsWindowEnabled check and the action call; MSAA provider returns a COM error for the default action; automating non-standard controls whose default action fails.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- ElementNotAvailableException
- ElementNotEnabledException
- SR.OperationCannotBePerformed
- Win32Exception
- Win32Exception(Marshal.GetLastWin32Error())
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ca165b62e0d10e2b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/MSAANativeProvider.cs:1210
throw new ElementNotEnabledException();
}
// If Toggle is ever supported for menu items then SetFocus may not be
// appropriate here as that may have the side-effect of closing the menu
Misc.SetFocus(_hwnd);
try
{
_acc.DoDefaultAction();
}
catch (Exception e)
{
if (Misc.IsCriticalException(e))
{
throw;
}
throw new InvalidOperationException(SR.OperationCannotBePerformed, e);
}
}
#endregion Private Methods
//------------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
#region Private Fields
//private delegate AutomationPattern PatternChecker(Accessible acc);
// a struct holding an entry for the table below
private struct RoleCtrlTypeView on GitHub (pinned to 81131a70a4)