dotnet/wpf · error · NotSupportedException

Stream does not support SetLength

Error message

Stream does not support SetLength

What it means

The writer-side XamlStream overrides Stream.SetLength(long) to throw NotSupportedException. The stream's length is managed internally by the ReadWriteStreamManager as bytes are written; callers cannot resize it directly, so the API contract deliberately rejects SetLength.

Solutions

  1. Remove the SetLength call; the stream grows automatically as Write/Seek are used via StreamManager.
  2. Check stream.CanWrite and avoid resize logic for non-file streams; guard with a capability check before resizing.
  3. If truncation semantics are required, write to a temporary MemoryStream or FileStream, then hand the finished buffer to the XAML pipeline.
  4. Catch NotSupportedException around resize operations and fall back to append-only writing.

Example fix

// before
xamlStream.SetLength(0); // NotSupportedException
// after
if (xamlStream.CanWrite)
{
    xamlStream.Seek(0, SeekOrigin.Begin); // overwrite in place; do not resize
}
Defensive patterns

Strategy: validation

Validate before calling

if (!stream.CanWrite) throw new InvalidOperationException("Cannot resize a non-writable stream.");
// XamlStream additionally forbids SetLength: avoid resizing entirely and Seek instead.

Type guard

static bool IsResizable(Stream s) => s is FileStream or MemoryStream; // XamlStream is not resizable

Try / catch

try { stream.SetLength(value); }
catch (NotSupportedException) { stream.Seek(0, SeekOrigin.Begin); /* overwrite in place */ }

Prevention

When it happens

Trigger: Calling SetLength(value) on the writer-side XamlStream, directly or via helpers such as FileStream-style truncation logic, defragmenting writers, or serializers that pre-size/truncate streams. Source: XamlStream.cs:684-687.

Common situations: Generic stream-processing utility code that truncates or pre-allocates streams before writing; refactoring existing file-based code to write into the XAML stream without removing SetLength calls.

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

Appendix: source

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

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


        /// <summary>
        /// Override of Stream.Write
        /// </summary>
        public override void Write(byte[] buffer, int offset, int count)
        {
            StreamManager.Write(buffer,offset,count);
        }

        #endregion overrides


        #region Methods

        /// <summary>
        /// Called by the writer to say its okay to let the reader see what

View on GitHub (pinned to 81131a70a4)