dotnet/wpf · error · NotSupportedException

Stream does not support Seek.

Error message

Stream does not support Seek.

What it means

ByteStream.Seek calls CheckDisposedStatus then verifies the CanSeek property before delegating to the wrapped IStream. If the stream cannot seek (CanSeek is false, e.g. the underlying IStream is null/unavailable), it throws NotSupportedException(SR.SeekNotSupported). The operation is simply not supported by this stream instance.

Solutions

  1. Check stream.CanSeek before calling Seek and fall back to sequential reading when it is false.
  2. Construct the ByteStream (or the package) over a seekable source such as a FileStream or MemoryStream.
  3. Ensure the ByteStream is not disposed and its IStream was properly provided at construction.

Example fix

// before
stream.Seek(partOffset, SeekOrigin.Begin);
// after
if (stream.CanSeek) stream.Seek(partOffset, SeekOrigin.Begin);
else ReadSequentially(stream, partOffset);
Defensive patterns

Strategy: validation

Validate before calling

if (!stream.CanSeek) throw new InvalidOperationException("Seek requires a seekable stream");

Type guard

bool IsSeekable(Stream s) => s is ByteStream ? s.CanSeek : s.CanSeek;

Try / catch

try { stream.Seek(off, SeekOrigin.Begin); }
catch (NotSupportedException) { /* fall back to sequential read */ }

Prevention

When it happens

Trigger: Calling Seek(offset, origin) on a ByteStream whose CanSeek is false - typically when the underlying security-suppressed IStream was never supplied or became unavailable.

Common situations: Code written against a generic Stream abstraction that assumes Seek works, applied to a ByteStream constructed over a non-seekable IStream; random-access readers (ZipPackage, XPS document readers) consuming such a stream.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs:190

        }

        /// <summary>
        /// Sets the position within the current stream.
        /// </summary>
        /// <remarks>
        /// ByteStream relies on underlying COM interface to
        /// validate offsets.
        /// </remarks>
        /// <param name="offset">Offset byte count</param>
        /// <param name="origin">Offset origin</param>
        /// <returns>The new position within the current stream.</returns>
        public override long Seek(long offset, SeekOrigin origin)
        {
            CheckDisposedStatus();

            if (!CanSeek)
            {
                throw new NotSupportedException(SR.SeekNotSupported);
            }

            long seekPos = 0;
            int translatedSeekOrigin = 0;

            switch (origin)
            {
                case SeekOrigin.Begin:
                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_SET;
                    if (0 > offset)
                    {
                        throw new ArgumentOutOfRangeException(nameof(offset),
                                                              SR.SeekNegative);
                    }
                    break;

                case SeekOrigin.Current:
                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_CUR;

View on GitHub (pinned to 81131a70a4)