dotnet/maui · error · NotSupportedException
The stream doesn't support modifications.
Error message
The stream doesn't support modifications.
What it means
The ReadonlyStream wrapper class (internal) delegates Read and Seek to an underlying stream but hard-rejects any mutation. SetLength is overridden to throw NotSupportedException because this stream advertises CanWrite=false and exists solely to expose a read-only view of another Stream. Any caller that attempts to resize the stream will hit this guard.
Source
Thrown at src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/Standard/StreamHelper.cs:607
set
{
_stream.Position = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
return _stream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return _stream.Seek(offset, origin);
}
public override void SetLength(long value)
{
throw new NotSupportedException("The stream doesn't support modifications.");
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException("The stream doesn't support modifications.");
}
public override void Close()
{
base.Close();
}
}
/// <summary>
/// Wraps a string to provide read-only Stream semantics.
/// </summary>
internal class StringStream : Stream
{View on GitHub (pinned to f377ff1c5e)
Solutions
- Check stream.CanWrite before calling SetLength; skip the call or branch to a writable stream when it returns false.
- If you own the stream construction, pass in a MemoryStream or FileStream opened with write access instead of letting the library wrap it in ReadonlyStream.
- Refactor the consuming code to not depend on SetLength for read-only sources.
Example fix
// before
stream.SetLength(newSize);
// after
if (stream.CanWrite)
{
stream.SetLength(newSize);
} Defensive patterns
Strategy: validation
Validate before calling
// Before calling SetLength
if (!stream.CanWrite)
{
// skip or use a writable stream
throw new InvalidOperationException("Stream does not support SetLength (read-only).");
}
stream.SetLength(newSize); Prevention
- Always check CanWrite before calling SetLength or Write on a stream received from an external API.
- Document which APIs return read-only wrappers so callers know to check capability flags.
- Prefer MemoryStream for intermediate buffers that need mutation.
When it happens
Trigger: Calling SetLength(value) on an instance of the internal ReadonlyStream class (the read-only wrapper constructed around another Stream). This happens when generic code that holds a Stream reference invokes SetLength without first checking CanWrite.
Common situations: Serialization or compression pipelines that accept a Stream and call SetLength to pre-allocate; caching layers that attempt to truncate; code that received the stream from an API returning a read-only wrapper without realizing CanWrite is false.
Related errors
- The streams can't be read for comparison
- Bad value for origin
- Invalid enumeration value
- No file exists at "{0}"
- DataTemplateSelector.OnSelectTemplate must not return anothe
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/d9e48a975ae1a369.
Report an issue: GitHub.