{"record":{"id":"cdbdebd23d726dff","repo":"dotnet/wpf","slug":"resourcenotfoundundercacheonlypolicy","errorCode":null,"errorMessage":"ResourceNotFoundUnderCacheOnlyPolicy","messagePattern":"ResourceNotFoundUnderCacheOnlyPolicy","errorType":"exception","errorClass":"WebException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/IO/Packaging/PackWebRequest.cs","lineNumber":133,"sourceCode":"            {\n                // inspect and act on CachePolicy\n                RequestCacheLevel policy = _cachePolicy.Level;\n                if (policy == RequestCacheLevel.Default)\n                    policy = _defaultCachePolicy.Level;\n\n                switch (policy)\n                {\n                    case RequestCacheLevel.BypassCache:\n                        {\n                            // ignore cache entry\n                            cachedPackageAvailable = false;\n                        } break;\n\n                    case RequestCacheLevel.CacheOnly:\n                        {\n                            // only use cached value\n                            if (!cachedPackageAvailable)\n                                throw new WebException(SR.ResourceNotFoundUnderCacheOnlyPolicy);\n                        } break;\n\n                    case RequestCacheLevel.CacheIfAvailable:\n                        {\n                            // use cached value if possible - we need take no explicit action here\n                        } break;\n\n                    default:\n                        {\n                            throw new WebException(SR.PackWebRequestCachePolicyIllegal);\n                        }\n                }\n            }\n            \n            if (cachedPackageAvailable)\n            {\n#if DEBUG\n                if (PackWebRequestFactory._traceSwitch.Enabled)","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/IO/Packaging/PackWebRequest.cs#L115-L151","documentation":"PackWebRequest.GetResponse throws WebException with SR.ResourceNotFoundUnderCacheOnlyPolicy when RequestCachePolicy is CacheOnly and no cached pack response (cachedPackageAvailable) exists. Under CacheOnly the request never goes to the network or package store; only an already-cached value may be served.","triggerScenarios":"Issuing a pack:// web request with RequestCacheLevel.CacheOnly (or default cache policy resolving to CacheOnly) when the resource was never downloaded/cached before, e.g. requesting a remote pack URI offline with CacheOnly policy.","commonSituations":"Offline-first scenarios with BindTo Nothing/CacheOnly-style policies, applications setting Application.CachePolicy or per-request CachePolicy to CacheOnly expecting previously loaded resources, or first-access of a resource while CacheOnly is active.","solutions":["Use RequestCacheLevel.CacheIfAvailable or Default instead of CacheOnly so the request can fall back to the package/network.","Pre-populate the cache by loading the resource once with a network-permitting policy before switching to CacheOnly.","Catch WebException for this policy and provide a local fallback resource.","Verify the URI targets a resource that is actually cached (e.g. inside the application package) rather than a remote location."],"exampleFix":"// before\nrequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheOnly);\nvar resp = request.GetResponse();\n// after\nrequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable);\nvar resp = request.GetResponse();","handlingStrategy":"try-catch","validationCode":"if (request.CachePolicy?.Level == RequestCacheLevel.CacheOnly)\n{\n    // only safe for resources known to be already cached\n}","typeGuard":null,"tryCatchPattern":"try { return request.GetResponse(); }\ncatch (WebException ex)\n{\n    request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable);\n    return request.GetResponse();\n}","preventionTips":["Reserve CacheOnly for resources you know are cached; prefer CacheIfAvailable/Default.","Pre-warm the cache before enabling CacheOnly policy.","Provide local fallback resources for offline scenarios."],"tags":["web-exception","cache-policy","pack-webrequest","wpf","offline"],"backgroundTag":"resource-not-found","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"}