dotnet/wpf · error · WebException
ResourceNotFoundUnderCacheOnlyPolicy
Error message
ResourceNotFoundUnderCacheOnlyPolicy
What it means
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.
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.
Example fix
// before request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheOnly); var resp = request.GetResponse(); // after request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable); var resp = request.GetResponse();
Defensive patterns
Strategy: try-catch
Validate before calling
if (request.CachePolicy?.Level == RequestCacheLevel.CacheOnly)
{
// only safe for resources known to be already cached
} Try / catch
try { return request.GetResponse(); }
catch (WebException ex)
{
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable);
return request.GetResponse();
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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
- PackWebRequestCachePolicyIllegal
- requestUri.ToString() (WebExceptionStatus.RequestCanceled)
- SR.Format(SR.General_BadType, "ConvertFrom")
- " }} " element found. Expected fixed page element ( }} ).
- ' ' can only host a ' ' or a ' '. ' ' is an invalid…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/cdbdebd23d726dff.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/IO/Packaging/PackWebRequest.cs:133
{
// inspect and act on CachePolicy
RequestCacheLevel policy = _cachePolicy.Level;
if (policy == RequestCacheLevel.Default)
policy = _defaultCachePolicy.Level;
switch (policy)
{
case RequestCacheLevel.BypassCache:
{
// ignore cache entry
cachedPackageAvailable = false;
} break;
case RequestCacheLevel.CacheOnly:
{
// only use cached value
if (!cachedPackageAvailable)
throw new WebException(SR.ResourceNotFoundUnderCacheOnlyPolicy);
} break;
case RequestCacheLevel.CacheIfAvailable:
{
// use cached value if possible - we need take no explicit action here
} break;
default:
{
throw new WebException(SR.PackWebRequestCachePolicyIllegal);
}
}
}
if (cachedPackageAvailable)
{
#if DEBUG
if (PackWebRequestFactory._traceSwitch.Enabled)View on GitHub (pinned to 81131a70a4)