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
- Don't call SetLength on SharedStream — write to the underlying base stream instead if mutation is required.
- Copy the window into a writable MemoryStream/FileStream and operate on that copy.
- Restructure code so BAML is treated as immutable input (read-only parsing).
- 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
- Treat SharedStream as immutable input — parse, never mutate
- Copy to MemoryStream/FileStream when modification is needed
- Check CanWrite before write-oriented APIs on streams
- Document stream ownership so writers don't receive read-only slices
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
- SR.SetLengthNotSupported
- SR.SetPositionNotSupported
- Stream does not support SetLength.
- Stream does not support Write
- Stream does not support writing.
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)