dotnet/wpf · error · NotSupportedException

SR.ReadNotSupported

Error message

SR.ReadNotSupported

What it means

PackagingUtilities.VerifyStreamReadArgs performs common argument validation for Stream.Read calls in the packaging layer. If the stream is not readable (CanRead is false, e.g. a write-only stream), it throws NotSupportedException(SR.ReadNotSupported).

Solutions

  1. Open the stream/part with FileAccess.Read or FileAccess.ReadWrite before reading.
  2. Check stream.CanRead before calling Read and use a separate readable stream if needed.
  3. Reopen the package part rather than reusing a write-only stream.

Example fix

// before
var part = pkg.CreatePart(uri, mime, CompressionOption.Normal); // write-only usage
part.GetStream(FileMode.Create, FileAccess.Write).Read(...);
// after
var s = part.GetStream(FileMode.Open, FileAccess.Read);
if (s.CanRead) s.Read(buffer, 0, buffer.Length);
Defensive patterns

Strategy: validation

Validate before calling

if (!stream.CanRead)
    throw new InvalidOperationException("stream must be readable before Read");

Type guard

bool Readable(Stream s) => s != null && s.CanRead;

Try / catch

try { stream.Read(buffer, offset, count); }
catch (NotSupportedException) { /* open a readable stream instead */ }

Prevention

When it happens

Trigger: Calling a packaging Stream.Read (or Read-related API such as reading a ZipPackage part) on a stream opened for write-only access, or a stream whose CanRead is false (closed/disposed/read-only-direction pipe).

Common situations: Opening a part with FileMode.Create/FileAccess.Write then attempting to read it; reading from a write-only FileStream; using a stream from a response/request direction that cannot read.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs:105

            //the encoding attribute in XmlDeclaration have already been ruled out by the check above.
            //Note: If not encoding attribute is present or no byte order marking is present the
            //encoding default to UTF8
            if (!(reader.Encoding is UnicodeEncoding || reader.Encoding is UTF8Encoding))
                throw new FileFormatException(SR.EncodingNotSupported);
        }

        /// <summary>
        /// VerifyStreamReadArgs
        /// </summary>
        /// <param name="s">stream</param>
        /// <param name="buffer">buffer</param>
        /// <param name="offset">offset</param>
        /// <param name="count">count</param>
        /// <remarks>Common argument verification for Stream.Read()</remarks>
        internal static void VerifyStreamReadArgs(Stream s, byte[] buffer, int offset, int count)
        {
            if (!s.CanRead)
                throw new NotSupportedException(SR.ReadNotSupported);

            ArgumentNullException.ThrowIfNull(buffer);

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), SR.OffsetNegative);
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), SR.ReadCountNegative);
            }

            checked     // catch any integer overflows
            {
                if (offset + count > buffer.Length)
                {
                    throw new ArgumentException(SR.ReadBufferTooSmall, nameof(buffer));

View on GitHub (pinned to 81131a70a4)