dotnet/wpf · error · NotSupportedException

Stream does not support reading.

Error message

Stream does not support reading.

What it means

ByteStream.Read first checks disposal and then the CanRead property, throwing NotSupportedException(SR.ReadNotSupported) when the stream cannot read. This surfaces when the underlying security-suppressed IStream is missing or the stream was constructed in a write/none-access mode.

Solutions

  1. Check stream.CanRead before reading and surface a clear error if false.
  2. Ensure the ByteStream is constructed with a valid IStream and is not disposed before use.
  3. Fix ownership/disposal so consumers do not read a closed stream.

Example fix

// before
int n = stream.Read(buf, 0, buf.Length);
// after
if (!stream.CanRead) throw new InvalidOperationException("Stream is not readable");
int n = stream.Read(buf, 0, buf.Length);
Defensive patterns

Strategy: validation

Validate before calling

if (!stream.CanRead) throw new InvalidOperationException("Stream is not readable");

Try / catch

try { int n = stream.Read(buf, 0, buf.Length); }
catch (NotSupportedException) { /* re-open the stream with read access */ }

Prevention

When it happens

Trigger: Calling Read(buffer, offset, count) on a ByteStream whose CanRead is false - typically a disposed or IStream-less instance, or one opened without read access.

Common situations: Package readers handed a ByteStream opened write-only; lifecycle bugs where the stream was closed but references linger; generic Stream-consuming code assuming Read always works.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs:250

        {
            throw new NotSupportedException(SR.SetLengthNotSupported);
        }

        /// <summary>
        /// Reads a sequence of bytes from the current stream and advances the 
        /// position within the stream by the number of bytes read.
        /// </summary>
        /// <param name="buffer">Read data buffer</param>
        /// <param name="offset">Buffer start position</param>
        /// <param name="count">Number of bytes to read</param>
        /// <returns>Number of bytes actually read</returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            CheckDisposedStatus();

            if (!CanRead)
            {
                throw new NotSupportedException(SR.ReadNotSupported);
            }
            
            int read = 0;

            // optimization: if we are being asked to read zero bytes, be done.
            if (count == 0)
            {
                return read;
            }
            
            // count has to be positive number
            if (0 > count)
            {
                throw new ArgumentOutOfRangeException(nameof(count),
                                                      SR.ReadCountNegative);
            }

            // offset has to be a positive number

View on GitHub (pinned to 81131a70a4)