dotnet/wpf · error · WebException

PackWebRequestCachePolicyIllegal

Error message

PackWebRequestCachePolicyIllegal

What it means

PackWebRequest.GetResponse validates the assigned cache policy before issuing a request. Only BypassCache, CacheOnly, and CacheIfAvailable RequestCacheLevel values are legal for pack:// requests; any other level (e.g. Default, NoCacheNoStore, Reload) causes a WebException wrapping SR.PackWebRequestCachePolicyIllegal. The pack scheme is served from the in-process PackageStore or a nested stream, so arbitrary HTTP-style cache directives cannot be honored.

Solutions

  1. Set CachePolicy to new RequestCachePolicy(RequestCacheLevel.BypassCache) (or CacheOnly / CacheIfAvailable) before calling GetResponse.
  2. Do not mutate CachePolicy on pack WebRequests; leave the default policy in place.
  3. If custom caching is required, cache at the application layer (e.g. store the Package in PackageStore) instead of via cache policy.

Example fix

// before
req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Default);
var resp = req.GetResponse();
// after
req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
var resp = req.GetResponse();
Defensive patterns

Strategy: validation

Validate before calling

var policy = req.CachePolicy;
if (req is PackWebRequest && policy != null &&
    policy.Level != RequestCacheLevel.BypassCache &&
    policy.Level != RequestCacheLevel.CacheOnly &&
    policy.Level != RequestCacheLevel.CacheIfAvailable)
    req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);

Try / catch

try { var resp = req.GetResponse(); }
catch (WebException ex) when (ex.Message.Contains("cache policy")) { req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache); var resp = req.GetResponse(); }

Prevention

When it happens

Trigger: Calling PackWebRequest.GetResponse after assigning a CachePolicy (or a WebRequest whose cache level was set via CachePolicy/DefaultCachePolicy) whose RequestCacheLevel is not BypassCache, CacheOnly, or CacheIfAvailable.

Common situations: Code that reuses generic HTTP client helpers which set CachePolicy = HttpRequestCacheLevel-based or RequestCacheLevel.Default on all WebRequests before fetching pack:// resources; porting code that worked with plain HttpWebRequest.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/IO/Packaging/PackWebRequest.cs:143

                            // 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)
                    System.Diagnostics.Trace.TraceInformation(
                        DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " +
                        Environment.CurrentManagedThreadId + ": " + 
                        "PackWebRequest - Getting response from Package Cache");
#endif
                return new PackWebResponse(_uri, _innerUri, _partName, _cacheEntry, _cachedPackageIsThreadSafe);
            }
            else
            {
                // only return a real WebRequest instance - throw on a PseudoWebRequest

View on GitHub (pinned to 81131a70a4)