dotnet/wpf · error · InvalidOperationException

SR.CacheRequestNeedElementReference

Error message

SR.CacheRequestNeedElementReference

What it means

CheckElement validates that the AutomationElement holds a valid native UIA node handle (_hnode). If the handle is null or invalid the element cannot back any cache request or navigation, so it throws InvalidOperationException(SR.CacheRequestNeedElementReference).

Solutions

  1. Re-acquire the element (FindFirst/FromHandle) instead of reusing the stale instance
  2. Check that the target window/application is still alive before using the element
  3. Catch InvalidOperationException and rebuild the element reference

Example fix

// before
staleElement.CheckElement(); // throws if handle invalid
// after
AutomationElement element = AutomationElement.FromHandle(hwnd)
    ?? await ReacquireElementAsync();
Defensive patterns

Strategy: validation

Validate before calling

static bool HasValidHandle(AutomationElement e) =>
    e is AutomationElement el && el.IsHandleValid(); // wrap CheckElement in a non-throwing check

Type guard

null

Try / catch

try { el.CheckElement(); }
catch (InvalidOperationException) { el = ReacquireElement(originalQuery); }

Prevention

When it happens

Trigger: Using an AutomationElement whose runtime node handle was never initialized or has become invalid — e.g. an element constructed around a disposed/invalid SafeNodeHandle, or an element for a node that was removed from the UIA tree.

Common situations: Holding element references across window teardown; caching elements whose target window/application exited; marshaling elements across threads or reusing stale cached elements.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/de0e962eae22bb39. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/AutomationElement.cs:1104

        }


        #endregion Public Properties


        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
 
        #region Internal Methods

        internal void CheckElement()
        {
            if (_hnode == null || _hnode.IsInvalid)
            {
                throw new InvalidOperationException(SR.CacheRequestNeedElementReference);
            }
        }

        // Called by the treewalker classes to navigate - we call through to the
        // provider wrapper, which gets the navigator code to do its stuff
        internal AutomationElement Navigate(NavigateDirection direction, Condition condition, CacheRequest request)
        {
            CheckElement();

            UiaCoreApi.UiaCacheRequest cacheRequest;
            if (request == null)
                cacheRequest = CacheRequest.DefaultUiaCacheRequest;
            else
                cacheRequest = request.GetUiaCacheRequest();

            UiaCoreApi.UiaCacheResponse response = UiaCoreApi.UiaNavigate(_hnode, direction, condition, cacheRequest);
            return CacheHelper.BuildAutomationElementsFromResponse(cacheRequest, response);
        }

View on GitHub (pinned to 81131a70a4)