dotnet/wpf · error · WebException

WebResponsePartNotFound

Error message

WebResponsePartNotFound

What it means

When a PackWebResponse is backed by a network stream (the pack URI had an outer http/file transport), GetResponseStream opens the downloaded container and looks up the requested part name. If the container has no part matching _partName, it throws WebException(SR.WebResponsePartNotFound). The same check underlies the ContentType and ContentLength properties, so accessing those on a missing part also fails.

Solutions

  1. Verify the part exists: Package.Open(stream) then c.PartExists(partName) before consuming the response.
  2. Correct the part name in the pack URI (it must match the package's part paths exactly, including case and extension).
  3. Check that the server actually serves the expected package version containing the part.

Example fix

// before
var s = resp.GetResponseStream();
// after
using (Package c = Package.Open(resp.GetResponseStream()))
    if (!c.PartExists(packUri part)) throw ... // or choose another part
Defensive patterns

Strategy: try-catch

Validate before calling

using (Package c = Package.Open(stream))
    bool exists = c.PartExists(new Uri(partName, UriKind.Relative));

Try / catch

try { return resp.GetResponseStream(); }
catch (WebException ex) when (ex.Message.Contains("part")) { /* part missing: check URI/part name or package version */ throw; }

Prevention

When it happens

Trigger: Requesting a pack URI whose referenced part does not exist inside the downloaded package, then calling GetResponseStream, ContentType, or ContentLength.

Common situations: Typos or case mismatches in the part path after pack://http:...; server package updated/renamed while client still requests an old part name; wrong content-type-derived part name in a nested package.

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/b0a5598460fa9165. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/IO/Packaging/PackWebResponse.cs:233

                    // wrap our stream for efficiency (short reads are expanded)
                    _responseStream = new BufferedStream(_responseStream);
                }

                // handle degenerate case where there is no part name
                if (_partName == null)
                {
                    _fullStreamLength = streamLength;    // entire container
                    _mimeType = WpfWebRequestHelper.GetContentType(_fullResponse);

                    // pass this so that ResponseStream holds a reference to us until the stream is closed
                    _responseStream = new ResponseStream(_responseStream, this);
                }
                else
                {
                    // open container on netStream
                    Package c = Package.Open(_responseStream);
                    if (!c.PartExists(_partName))
                        throw new WebException(SR.WebResponsePartNotFound);

                    PackagePart p = c.GetPart(_partName);

                    Stream s = p.GetSeekableStream(FileMode.Open, FileAccess.Read);

                    _mimeType = new MS.Internal.ContentType(p.ContentType);      // save this for use in ContentType property - may still be null
                    _fullStreamLength = s.Length;   // just this stream

                    // Wrap in a ResponseStream so that this container will be released
                    // when the stream is closed
                    _responseStream = new ResponseStream(s, this, _responseStream, c);
                }

                // length available? (-1 means the server chose not to report it)
                if (_fullStreamLength >= 0)
                {
                    _lengthAvailable = true;
                }

View on GitHub (pinned to 81131a70a4)