{"record":{"id":"a016f8268d0619fc","repo":"dotnet/wpf","slug":"sr-cacherequestneedcache","errorCode":null,"errorMessage":"SR.CacheRequestNeedCache","messagePattern":"SR\\.CacheRequestNeedCache","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/Misc.cs","lineNumber":183,"sourceCode":"        {\n            throw new ArgumentException(SR.Format(SR.GenericInvalidArgument, argName));\n        }\n\n        // Check that specified condition is true; if not, throw exception\n        internal static void ValidateArgument(bool cond, string reason)\n        {\n            if (!cond)\n            {\n                throw new ArgumentException(SR.GetResourceString(reason, null));\n            }\n        }\n\n        // Called by the patterns before accessing .Cache\n        internal static void ValidateCached(bool cached)\n        {\n            if (!cached)\n            {\n                throw new InvalidOperationException(SR.CacheRequestNeedCache);\n            }\n        }\n\n        // Called by the patterns before accessing .Current\n        internal static void ValidateCurrent(SafePatternHandle hPattern)\n        {\n            if (hPattern.IsInvalid)\n            {\n                throw new InvalidOperationException(SR.CacheRequestNeedLiveForProperties);\n            }\n        }\n\n        // Call IsCriticalException w/in a catch-all-exception handler to allow critical exceptions\n        // to be thrown (this is copied from exception handling code in WinForms but feel free to\n        // add new critical exceptions).  Usage:\n        //      try\n        //      {\n        //          Somecode();","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/Misc.cs#L165-L201","documentation":"Misc.ValidateCached throws InvalidOperationException(SR.CacheRequestNeedCache) when a pattern's Cached property is accessed without a cache request having been made. UIA patterns only expose Cached data when the element/pattern was fetched with an active CacheRequest.","triggerScenarios":"Accessing pattern.Cached on a pattern object obtained without a CacheRequest (e.g., GetCurrentPattern instead of GetCachedPattern or FindFirst with a CacheRequest), or with CacheRequest made default not used during retrieval.","commonSituations":"Developers mixing Current and Cached access: they retrieve patterns without caching options then read .Cached for performance reasons; typical when copying code that used a CacheRequest elsewhere.","solutions":["Retrieve the element/pattern within an active CacheRequest (AutomationElement.FindFirst with cacheRequest, or cacheRequest.GetUpdatedCache) before accessing .Cached.","Use AutomationElement.GetCachedPattern or pass the cache request so the pattern handle carries cached data.","If live data is acceptable, switch to pattern.Current instead of pattern.Cached."],"exampleFix":"// before\nvar pat = (ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern);\nvar v = pat.Cached.Value; // throws\n// after\nvar req = new CacheRequest { TreeScope = TreeScope.Element };\nreq.Add(ValuePattern.Pattern);\nvar cached = element.FindFirst(TreeScope.Element, req);\nvar v = ((ValuePattern)cached.GetCachedPattern(ValuePattern.Pattern)).Cached.Value;","handlingStrategy":"validation","validationCode":"var req = new CacheRequest(); req.Add(ValuePattern.Pattern);\nvar el = root.FindFirst(TreeScope.Children, req); // ensures .Cached is valid","typeGuard":"static bool HasCachedData(AutomationElement el, AutomationPattern p) => el != null && el.GetSupportedPatterns().Contains(p);","tryCatchPattern":"try { var v = pat.Cached.Value; }\ncatch (InvalidOperationException) { var v = ((ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern)).Current.Value; }","preventionTips":["Always fetch elements/patterns through a CacheRequest when you intend to read .Cached.","Never mix cached retrieval with .Cached access on patterns obtained via GetCurrentPattern.","Fall back to .Current when cached data is unavailable."],"tags":["uiautomation","cache-request","invalid-state"],"backgroundTag":"invalid-state-transition","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"}