{"record":{"id":"ca165b62e0d10e2b","repo":"dotnet/wpf","slug":"invalidoperationexception-sr-operationcannotbeperformed-e","errorCode":null,"errorMessage":"InvalidOperationException(SR.OperationCannotBePerformed, e)","messagePattern":"InvalidOperationException\\(SR\\.OperationCannotBePerformed, e\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/MSAANativeProvider.cs","lineNumber":1210,"sourceCode":"                throw new ElementNotEnabledException();\n            }\n\n            // If Toggle is ever supported for menu items then SetFocus may not be \n            // appropriate here as that may have the side-effect of closing the menu\n            Misc.SetFocus(_hwnd);\n\n            try\n            {\n                _acc.DoDefaultAction();\n            }\n            catch (Exception e)\n            {\n                if (Misc.IsCriticalException(e))\n                {\n                    throw;\n                }\n\n                throw new InvalidOperationException(SR.OperationCannotBePerformed, e);\n            }\n        }\n\n        #endregion Private Methods\n\n\n        //------------------------------------------------------\n        //\n        //  Private Fields\n        //\n        //------------------------------------------------------\n\n        #region Private Fields\n\n        //private delegate AutomationPattern PatternChecker(Accessible acc);\n\n        // a struct holding an entry for the table below\n        private struct RoleCtrlType","sourceCodeStart":1192,"sourceCodeEnd":1228,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/MSAANativeProvider.cs#L1192-L1228","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ntry { invokePattern.Invoke(); }\ncatch (InvalidOperationException) { /* cause lost */ }\n// after\ntry\n{\n    invokePattern.Invoke();\n}\ncatch (InvalidOperationException ex)\n{\n    Console.WriteLine($\"Action failed: {ex.InnerException?.Message ?? ex.Message}\");\n    element = FindElementAgain(); // re-acquire stale reference\n}","handlingStrategy":"try-catch","validationCode":"// Re-verify the element exists and is enabled before invoking:\nif (element == null || !(bool)element.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty)) return;","typeGuard":"bool IsInvokable(AutomationElement e) => e?.GetCurrentPattern(InvokePattern.Pattern) is InvokePattern;","tryCatchPattern":"try { invokePattern.Invoke(); } catch (InvalidOperationException ex) { Log(ex.InnerException); ReacquireAndRetry(); }","preventionTips":["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"],"tags":["ui-automation","invoke-pattern","operation-failed","win32"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}