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

  1. Use RequestCacheLevel.CacheIfAvailable or Default instead of CacheOnly so the request can fall back to the package/network.
  2. Pre-populate the cache by loading the resource once with a network-permitting policy before switching to CacheOnly.
  3. Catch WebException for this policy and provide a local fallback resource.
  4. 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

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


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)