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
- Set CachePolicy to new RequestCachePolicy(RequestCacheLevel.BypassCache) (or CacheOnly / CacheIfAvailable) before calling GetResponse.
- Do not mutate CachePolicy on pack WebRequests; leave the default policy in place.
- 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
- Never set CachePolicy on pack:// WebRequests
- Centralize request creation so pack requests skip HTTP-oriented policy setup
- Restrict shared request helpers to only the three allowed cache levels
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
- SchemaInvalidForTransport
- ' ' cannot contain the path delimiter: ' '.
- ' ' cannot start with the reserved character range…
- ' ' ID is not a valid XSD ID.
- ' ' is not a valid value for ' '.
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 PseudoWebRequestView on GitHub (pinned to 81131a70a4)