{"record":{"id":"a8845ec0a523e38b","repo":"dotnet/wpf","slug":"stream-does-not-support-setlength-xamlstream","errorCode":null,"errorMessage":"Stream does not support SetLength","messagePattern":"Stream does not support SetLength","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs","lineNumber":686,"sourceCode":"        public override int ReadByte()\n        {\n            throw new NotSupportedException();\n        }\n\n        /// <summary>\n        /// Override of Stream.Seek\n        /// </summary>\n        public override long Seek(long offset, SeekOrigin loc)\n        {\n            return StreamManager.WriterSeek(offset,loc);\n        }\n\n        /// <summary>\n        /// Override of Stream.SetLength\n        /// </summary>\n        public override void SetLength(long value)\n        {\n            throw new NotSupportedException();\n        }\n\n\n        /// <summary>\n        /// Override of Stream.Write\n        /// </summary>\n        public override void Write(byte[] buffer, int offset, int count)\n        {\n            StreamManager.Write(buffer,offset,count);\n        }\n\n        #endregion overrides\n\n\n        #region Methods\n\n        /// <summary>\n        /// Called by the writer to say its okay to let the reader see what","sourceCodeStart":668,"sourceCodeEnd":704,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs#L668-L704","documentation":"The writer-side XamlStream overrides Stream.SetLength(long) to throw NotSupportedException. The stream's length is managed internally by the ReadWriteStreamManager as bytes are written; callers cannot resize it directly, so the API contract deliberately rejects SetLength.","triggerScenarios":"Calling SetLength(value) on the writer-side XamlStream, directly or via helpers such as FileStream-style truncation logic, defragmenting writers, or serializers that pre-size/truncate streams. Source: XamlStream.cs:684-687.","commonSituations":"Generic stream-processing utility code that truncates or pre-allocates streams before writing; refactoring existing file-based code to write into the XAML stream without removing SetLength calls.","solutions":["Remove the SetLength call; the stream grows automatically as Write/Seek are used via StreamManager.","Check stream.CanWrite and avoid resize logic for non-file streams; guard with a capability check before resizing.","If truncation semantics are required, write to a temporary MemoryStream or FileStream, then hand the finished buffer to the XAML pipeline.","Catch NotSupportedException around resize operations and fall back to append-only writing."],"exampleFix":"// before\nxamlStream.SetLength(0); // NotSupportedException\n// after\nif (xamlStream.CanWrite)\n{\n    xamlStream.Seek(0, SeekOrigin.Begin); // overwrite in place; do not resize\n}","handlingStrategy":"validation","validationCode":"if (!stream.CanWrite) throw new InvalidOperationException(\"Cannot resize a non-writable stream.\");\n// XamlStream additionally forbids SetLength: avoid resizing entirely and Seek instead.","typeGuard":"static bool IsResizable(Stream s) => s is FileStream or MemoryStream; // XamlStream is not resizable","tryCatchPattern":"try { stream.SetLength(value); }\ncatch (NotSupportedException) { stream.Seek(0, SeekOrigin.Begin); /* overwrite in place */ }","preventionTips":["Treat non-FileStream/MemoryStream streams as non-resizable by default.","Never pre-size or truncate streams you did not create.","Use Seek + overwrite instead of SetLength for fixed-layout writers.","Buffer into a MemoryStream when truncation semantics are required."],"tags":["wpf","stream","not-supported","xaml"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}