dotnet/wpf · error · NotSupportedException

Stream does not support Position setter

Error message

Stream does not support Position setter

What it means

The WriterStream nested in XamlStream (the write half of the internal parser pipe) intentionally does not support setting Position — its setter throws NotSupportedException. The position is managed internally by the XAML reader/writer, so external code cannot move it.

Solutions

  1. Do not set Position on this stream; write sequentially from the current position.
  2. Use the Seek method with SeekOrigin.Begin/Current if repositioning is required and supported.
  3. Buffer content in a MemoryStream first, then write it sequentially to this stream.

Example fix

// before
writerStream.Position = 0; // throws
// after
writerStream.Seek(0, SeekOrigin.Begin); // if seek is needed and supported
Defensive patterns

Strategy: try-catch

Validate before calling

bool canSetPosition = stream.CanSeek; // WriterStream reports CanSeek false in practice; guard before setting Position

Type guard

bool canSetPosition = stream is { CanSeek: true };

Try / catch

try { stream.Position = 0; }
catch (NotSupportedException) { /* stream is non-seekable; write sequentially instead */ }

Prevention

When it happens

Trigger: Assigning stream.Position on the writer stream obtained from XamlStream, or calling APIs that internally set Position (e.g. some copy/serialization helpers).

Common situations: Generic code that repositions streams before writing (e.g. stream.Position = 0 to overwrite) applied to the internal XAML writer stream.

Related errors


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

Appendix: source

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

        {
            get
            {
                return StreamManager.WriteLength;
            }
        }

        /// <summary>
        /// Override of Stream.Position
        /// </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();
        }

View on GitHub (pinned to 81131a70a4)