dotnet/wpf · error · NotSupportedException
Stream does not support Write
Error message
Stream does not support Write
What it means
ReaderStream overrides Stream.Write(byte[], int, int) to throw NotSupportedException because it is strictly a read-side view over the ReadWriteStreamManager; CanWrite returns false and all writes are rejected.
Solutions
- Write only to the writer-side XamlStream; treat ReaderStream as strictly read-only.
- Check stream.CanWrite before selecting it as a write destination.
- If both read and write are needed on the same data, use a MemoryStream or temporary file and pass writable views to each side.
- Catch NotSupportedException around write attempts on streams of unknown writability.
Example fix
// before
readerStream.Write(buffer, 0, buffer.Length); // NotSupportedException
// after
if (readerStream.CanWrite)
{
readerStream.Write(buffer, 0, buffer.Length);
}
else
{
throw new InvalidOperationException("ReaderStream is read-only; write via the writer stream.");
} Defensive patterns
Strategy: type-guard
Validate before calling
if (!stream.CanWrite) throw new InvalidOperationException("Cannot write to a read-only ReaderStream."); Type guard
static bool IsWritable(Stream s) => s != null && s.CanWrite;
Try / catch
try { stream.Write(buffer, 0, count); }
catch (NotSupportedException) { /* route writes to the writer-side stream */ } Prevention
- Gate every write with a CanWrite check.
- Never use a reader stream as a CopyTo/StreamWriter/encoder destination.
- Keep the reader/writer stream roles from the ReadWriteStreamManager strictly separated.
- In generic stream utilities, branch on CanWrite instead of assuming writability.
When it happens
Trigger: Calling Write (or WriteByte, or APIs like StreamWriter/BinaryWriter/CopyTo destinations that write) on the reader-side ReaderStream. Source: XamlStream.cs:857-860.
Common situations: Passing the reader stream as a destination to CopyTo, StreamWriter, serializers, or image encoders; utility code that writes a header back into a stream it is also reading.
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
- SR.SetLengthNotSupported
- Image_OriginalStreamReadOnly
- NotSupportedException
- NotSupportedException
- NotSupportedException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a3aa7f7a1e563570.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs:859
{
return StreamManager.ReaderSeek(offset,loc);
}
/// <summary>
/// Override of Stream.SetLength
/// </summary>
public override void SetLength(long value)
{
throw new NotSupportedException();
}
/// <summary>
/// Override of Stream.Write
/// </summary>
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
#endregion Overrides
#region Methods
/// <summary>
/// Called by the reader to indicate all bytes up to and
/// including the position are no longer needed
/// After making this call the reader cannot go back and
/// read this data
/// </summary>
/// <param name="position"></param>
internal void ReaderDoneWithFileUpToPosition(long position)
{
StreamManager.ReaderDoneWithFileUpToPosition(position);
}
View on GitHub (pinned to 81131a70a4)