dotnet/wpf · error · NotSupportedException

SR.SetPositionNotSupported

Error message

SR.SetPositionNotSupported

What it means

ByteStream wraps a COM IStream and implements a Stream-like Position setter. When the underlying stream is not seekable, setting Position throws NotSupportedException (SR.SetPositionNotSupported); writes go through CheckDisposedStatus first, so a disposed stream would already have thrown before this check.

Solutions

  1. Check ByteStream.CanSeek before setting Position and skip the seek when false.
  2. Copy the stream into a MemoryStream to obtain a seekable copy before random access.
  3. Read forward sequentially instead of repositioning.
  4. Ensure the source COM object exposed to ByteStream genuinely supports IStream::Seek.

Example fix

// before
stream.Position = 0;
// after
if (stream.CanSeek) { stream.Position = 0; } else { var copy = new MemoryStream(); stream.CopyTo(copy); copy.Position = 0; }
Defensive patterns

Strategy: type-guard

Validate before calling

bool seekable = stream is ByteStream bs && bs.CanSeek;

Type guard

static bool CanSeekSafe(Stream s) => s.CanSeek;

Try / catch

try { stream.Position = 0; }
catch (NotSupportedException) { /* forward-only: buffer or read sequentially */ }

Prevention

When it happens

Trigger: Assigning ByteStream.Position (or calling Stream APIs that set it, like Seek with an absolute offset) on a ByteStream whose CanSeek is false — typically a forward-only pipe/device IStream that does not support STREAM_SEEK_SET.

Common situations: Treating pack-URI streams from non-seekable COM sources like files (network pipes, printers, in-progress downloads) as random-access; buffering code that assumes every Stream supports seeking.

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

Appendix: source

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

            {
                CheckDisposedStatus();
                
                long seekPos = 0;

                _securitySuppressedIStream.Seek(0,
                                                      NativeMethods.STREAM_SEEK_CUR,
                                                      out seekPos);

                return seekPos;
            }

            set
            {
                CheckDisposedStatus();

                if (!CanSeek)
                {
                    throw new NotSupportedException(SR.SetPositionNotSupported);
                }
                
                long seekPos = 0;

                _securitySuppressedIStream.Seek(value,
                                                      NativeMethods.STREAM_SEEK_SET,
                                                      out seekPos);

                if (value != seekPos)
                {
                    throw new IOException(SR.SeekFailed);
                }
            }
        }

        #endregion Public Properties

View on GitHub (pinned to 81131a70a4)