dotnet/wpf · error · NotSupportedException
NotSupportedException
Error message
NotSupportedException
What it means
LazyFileStream in ResourcesGenerator is a read-only stream backed by a resource file opened with FileAccess.Read. SetLength is not a meaningful operation on a read-only file, so the override unconditionally throws NotSupportedException.
Solutions
- Do not call SetLength on this stream; use CanWrite/CanSeek checks before invoking write APIs.
- Copy the resource stream into a writable MemoryStream or FileStream and pass that to the write-oriented API.
- Wrap the stream in a ReadOnlyStream adapter that reports CanWrite=false so consumers never attempt writes.
Example fix
// before
writerStream.SetLength(0);
// after
using (var ms = new MemoryStream())
{
resourceStream.CopyTo(ms);
ms.SetLength(0); // operate on writable stream
} Defensive patterns
Strategy: type-guard
Validate before calling
if (!stream.CanWrite)
throw new InvalidOperationException("Stream is read-only; SetLength is not supported"); Type guard
bool IsWritable(Stream s) => s != null && s.CanWrite;
Try / catch
try { stream.SetLength(0); }
catch (NotSupportedException) { /* copy to writable stream instead */ } Prevention
- Check CanWrite/CanSeek before write-style operations on streams
- Wrap read-only streams so consumers see CanWrite=false
- Copy to MemoryStream when mutation is needed
When it happens
Trigger: Any call to SetLength(long) on the LazyFileStream instance returned by ResourcesGenerator (e.g. code treating the resource stream as writable, or a component like a serializer that calls SetLength before writing).
Common situations: Passing the read-only resource stream to APIs that expect a writable Stream (e.g. writers that truncate via SetLength), instead of supplying a MemoryStream or file-backed writable stream.
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
- 0x80041705
- 0x80041705
- Animation_NoTextChildren
- Animation_NoTextChildren
- Can only call SelectAll when SelectionMode is Multiple or…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/6d58b63956268578.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft/Build/Tasks/Windows/ResourcesGenerator.cs:93
{
get { return SourceStream.Position; }
set { SourceStream.Position = value; }
}
public override int Read(byte[] buffer, int offset, int count)
{
return SourceStream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return SourceStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
// This is backed by a readonly file.
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
// This is backed by a readonly file.
throw new NotSupportedException();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_sourceStream?.Dispose();
_sourceStream = null;
}
}
}
View on GitHub (pinned to 81131a70a4)