dotnet/wpf · error · NotSupportedException

Stream does not support SetLength.

Error message

Stream does not support SetLength.

What it means

NetStream is a read-only stream over a network/resource stream for package parts; it deliberately does not implement resizing, so SetLength always throws NotSupportedException (SR.SetLengthNotSupported). This library throws it because the underlying transport provides no truncation/extension capability.

Solutions

  1. Copy the part into a writable MemoryStream or FileStream and resize that instead
  2. Gate the code path with CanWrite/CanSeek checks and skip resizing for read-only streams
  3. Rewrite the package part with the desired size via Packaging APIs rather than resizing in place

Example fix

// before
destStream.SetLength(totalSize);
// after
if (destStream.CanWrite)
    CopyToResizableStream(destStream, totalSize); // copy into MemoryStream/FileStream
else
    throw new NotSupportedException("Read-only NetStream cannot be resized");
Defensive patterns

Strategy: type-guard

Validate before calling

if (!stream.CanWrite || !stream.CanSeek)
    throw new NotSupportedException("SetLength requires a writable, seekable stream; copy to MemoryStream first");

Type guard

static bool SupportsSetLength(Stream s) => s.CanWrite && s.CanSeek && s is not System.Net.Sockets.NetworkStream;

Try / catch

try { stream.SetLength(size); } catch (NotSupportedException) { using var ms = new MemoryStream(); stream.CopyTo(ms); ms.SetLength(size); /* use ms */ }

Prevention

When it happens

Trigger: Calling SetLength(long) on any NetStream instance — unconditionally thrown for every value of newLength.

Common situations: Generic stream-copy or resize helpers that call SetLength on the destination stream; shared code paths written for MemoryStream/FileStream reused against package-part streams.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/NetStream.cs:301

                    throw new ArgumentException(SR.SeekNegative);

#if DEBUG
                if (System.IO.Packaging.PackWebRequestFactory._traceSwitch.Enabled)
                    System.Diagnostics.Trace.TraceInformation("NetStream.set_Position() pos:{0}", value);
#endif

                _position = value;
            }
        }


        /// <summary>
        /// SetLength
        /// </summary>
        /// <exception cref="NotSupportedException">not supported</exception>
        public override void SetLength(long newLength)
        {
            throw new NotSupportedException(SR.SetLengthNotSupported);
        }


        /// <summary>
        /// Write
        /// </summary>
        /// <exception cref="NotSupportedException">not supported</exception>
        public override void Write(byte[] buf, int offset, int count)
        {
            throw new NotSupportedException(SR.WriteNotSupported);
        }


        /// <summary>
        /// Length
        /// </summary>
        public override long Length
        {

View on GitHub (pinned to 81131a70a4)