dotnet/wpf · error · NotSupportedException
Stream does not support Position setter
Error message
Stream does not support Position setter
What it means
The WriterStream nested in XamlStream (the write half of the internal parser pipe) intentionally does not support setting Position — its setter throws NotSupportedException. The position is managed internally by the XAML reader/writer, so external code cannot move it.
Solutions
- Do not set Position on this stream; write sequentially from the current position.
- Use the Seek method with SeekOrigin.Begin/Current if repositioning is required and supported.
- Buffer content in a MemoryStream first, then write it sequentially to this stream.
Example fix
// before writerStream.Position = 0; // throws // after writerStream.Seek(0, SeekOrigin.Begin); // if seek is needed and supported
Defensive patterns
Strategy: try-catch
Validate before calling
bool canSetPosition = stream.CanSeek; // WriterStream reports CanSeek false in practice; guard before setting Position
Type guard
bool canSetPosition = stream is { CanSeek: true }; Try / catch
try { stream.Position = 0; }
catch (NotSupportedException) { /* stream is non-seekable; write sequentially instead */ } Prevention
- Check CanSeek before assigning Position.
- Never reposition the XamlStream writer stream; write sequentially.
- Use Seek(SeekOrigin.Begin) instead of the Position setter when supported.
When it happens
Trigger: Assigning stream.Position on the writer stream obtained from XamlStream, or calling APIs that internally set Position (e.g. some copy/serialization helpers).
Common situations: Generic code that repositions streams before writing (e.g. stream.Position = 0 to overwrite) applied to the internal XAML writer stream.
Related errors
- ArgumentOutOfRangeException(nameof(offset))
- SR.ParserWriterNoSeekEnd
- SR.ParserWriterUnknownOrigin
- Stream does not support Read
- can’t seek on baseStream
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/4b9d44d54d89ca32.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs:653
{
get
{
return StreamManager.WriteLength;
}
}
/// <summary>
/// Override of Stream.Position
/// </summary>
public override long Position
{
get
{
return -1;
}
set
{
throw new NotSupportedException();
}
}
/// <summary>
/// Override of Stream.Read
/// </summary>
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
/// <summary>
/// Override of Stream.ReadByte
/// </summary>
public override int ReadByte()
{
throw new NotSupportedException();
}View on GitHub (pinned to 81131a70a4)