dotnet/wpf · error · NotSupportedException

NotSupportedException

Error message

NotSupportedException

What it means

SharedStream is a read-only stream over a shared byte range, so SetLength(long) unconditionally throws NotSupportedException at any call. Any attempt to resize the stream (directly or via Stream APIs such as GetBuffer-less writers) hits this sentinel error; callers must not attempt to modify the stream's length.

Solutions

  1. Don't call SetLength on SharedStream — write to the underlying base stream instead if mutation is required.
  2. Copy the window into a writable MemoryStream/FileStream and operate on that copy.
  3. Restructure code so BAML is treated as immutable input (read-only parsing).
  4. If you need a writable stream, construct your own FileStream rather than slicing an existing one.

Example fix

// before
sharedStream.SetLength(0); // NotSupportedException
// after
var buffer = new MemoryStream();
sharedStream.CopyTo(buffer);
buffer.SetLength(0);
Defensive patterns

Strategy: try-catch

Validate before calling

// Check writability/resize capability before resizing attempts
if (stream is SharedStream)
    throw new InvalidOperationException("SharedStream is a read-only window; copy to a writable stream first.");

Type guard

static bool CanResize(Stream s) => s is not SharedStream && s.CanWrite;

Try / catch

try
{
    stream.SetLength(0);
}
catch (NotSupportedException)
{
    // fall back: copy to a writable stream and resize the copy
    var copy = new MemoryStream();
    stream.Position = 0;
    stream.CopyTo(copy);
    copy.SetLength(0);
}

Prevention

When it happens

Trigger: Calling sharedStream.SetLength(value) directly, or via any API that resizes the stream (e.g. passing it to a writer that calls SetLength, like StreamWriter with truncate semantics or serialization code that grows the stream).

Common situations: Trying to append or truncate BAML in place through the shared window; passing the stream to code that assumes a fully writable FileStream-like stream.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs:251

                default:
                    throw new InvalidEnumArgumentException(nameof(origin), (int)origin, typeof(SeekOrigin));
            }

            if (newPosition < 0 || newPosition >= _length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
            }

            CheckDisposed();

            _position = newPosition;

            return _position;
        }

        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_baseStream != null)
                {
                    _refCount.Value--;
                    if (_refCount.Value < 1)
                    {
                        _baseStream.Close();
                    }
                    _refCount = null;
                    _baseStream = null;
                }
            }

View on GitHub (pinned to 81131a70a4)