dotnet/wpf · error · NotSupportedException

Stream does not support Read

Error message

Stream does not support Read

What it means

The WriterStream in XamlStream is write-only: its Read override unconditionally throws NotSupportedException. Any attempt to read bytes from this stream fails because it only feeds data from the BAML writer to the reader.

Solutions

  1. Read from the corresponding reader side of the XamlStream instead of the writer stream.
  2. Write to a MemoryStream if you later need to read the bytes back, and feed that to the API.
  3. Remove read operations from code paths that only own the writer stream.

Example fix

// before
int b = writerStream.ReadByte(); // throws
// after
writerStream.Write(data, 0, data.Length);
// read from the XamlStream reader side instead:
int b = xamlStreamReader.Read();
Defensive patterns

Strategy: type-guard

Validate before calling

if (!stream.CanRead) throw new InvalidOperationException("This stream is write-only");

Type guard

bool canRead = stream is { CanRead: true };

Try / catch

try { stream.Read(buffer, 0, count); }
catch (NotSupportedException) { /* use the reader side of the XamlStream instead */ }

Prevention

When it happens

Trigger: Calling Read/ReadByte (directly or via helpers like StreamReader, CopyTo, ToArray-style reads) on the XamlStream writer stream.

Common situations: Generic stream utilities (CopyTo, reading back what was written, hashing content) applied to the write half of the internal XAML parser pipe.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs:662

        /// </summary>
        public override long Position
        {
            get
            {
                return -1;
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        /// <summary>
        /// Override of Stream.Read
        /// </summary>
        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Override of Stream.ReadByte
        /// </summary>
        public override int ReadByte()
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Override of Stream.Seek
        /// </summary>
        public override long Seek(long offset, SeekOrigin loc)
        {
            return StreamManager.WriterSeek(offset,loc);
        }

View on GitHub (pinned to 81131a70a4)