dotnet/wpf · error · NotSupportedException

Stream does not support ReadByte

Error message

Stream does not support ReadByte

What it means

XamlStream (the writer-side half of WPF's internal ReadWriteStreamManager pair used during XAML BAML compilation) overrides Stream.ReadByte() to unconditionally throw NotSupportedException. The writer stream is write/seek-only: reading bytes is not a supported operation on it. The framework throws early and clearly rather than silently failing.

Solutions

  1. Do not read from the writer-side XamlStream; use the corresponding reader-side stream provided by the ReadWriteStreamManager for read operations.
  2. Check stream.CanRead before calling ReadByte() or handing the stream to a reader API.
  3. If a readable copy is needed, copy data out via the supported reader stream into a MemoryStream and read from that.
  4. If this happens inside third-party code, wrap the call in try/catch for NotSupportedException and supply an alternate readable stream.

Example fix

// before
int b = writerXamlStream.ReadByte(); // NotSupportedException
// after
if (writerXamlStream.CanRead)
{
    int b = writerXamlStream.ReadByte();
}
else
{
    using var ms = new MemoryStream();
    writerXamlStream.Seek(0, SeekOrigin.Begin);
    // read via the manager's reader stream instead
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (!stream.CanRead) throw new InvalidOperationException("Stream is write-only; ReadByte is not available.");

Type guard

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

Try / catch

try { int b = stream.ReadByte(); }
catch (NotSupportedException) { /* fall back to the manager's reader stream */ }

Prevention

When it happens

Trigger: Calling ReadByte() (or any Stream API that internally calls it, e.g. BinaryReader.ReadByte or IO.StreamReader over the stream) on the writer-side XamlStream obtained while WPF writes XAML/BAML content. Source: XamlStream.cs:668-671.

Common situations: Passing the writer stream to a reader-oriented API (BinaryReader, StreamReader, image/serializer codecs that probe bytes); code that treats the stream as bidirectional because CanRead checks are skipped; copying the stream with a byte-at-a-time reader.

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

Appendix: source

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

            {
                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);
        }

        /// <summary>
        /// Override of Stream.SetLength
        /// </summary>
        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }

View on GitHub (pinned to 81131a70a4)