{"record":{"id":"4b9d44d54d89ca32","repo":"dotnet/wpf","slug":"stream-does-not-support-position-setter","errorCode":null,"errorMessage":"Stream does not support Position setter","messagePattern":"Stream does not support Position setter","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs","lineNumber":653,"sourceCode":"        {\n            get\n            {\n                return StreamManager.WriteLength;\n            }\n        }\n\n        /// <summary>\n        /// Override of Stream.Position\n        /// </summary>\n        public override long Position\n        {\n            get\n            {\n                return -1;\n            }\n            set\n            {\n                throw new NotSupportedException();\n            }\n        }\n\n        /// <summary>\n        /// Override of Stream.Read\n        /// </summary>\n        public override int Read(byte[] buffer, int offset, int count)\n        {\n            throw new NotSupportedException();\n        }\n\n        /// <summary>\n        /// Override of Stream.ReadByte\n        /// </summary>\n        public override int ReadByte()\n        {\n            throw new NotSupportedException();\n        }","sourceCodeStart":635,"sourceCodeEnd":671,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs#L635-L671","documentation":"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.","triggerScenarios":"Assigning stream.Position on the writer stream obtained from XamlStream, or calling APIs that internally set Position (e.g. some copy/serialization helpers).","commonSituations":"Generic code that repositions streams before writing (e.g. stream.Position = 0 to overwrite) applied to the internal XAML writer stream.","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."],"exampleFix":"// before\nwriterStream.Position = 0; // throws\n// after\nwriterStream.Seek(0, SeekOrigin.Begin); // if seek is needed and supported","handlingStrategy":"try-catch","validationCode":"bool canSetPosition = stream.CanSeek; // WriterStream reports CanSeek false in practice; guard before setting Position","typeGuard":"bool canSetPosition = stream is { CanSeek: true };","tryCatchPattern":"try { stream.Position = 0; }\ncatch (NotSupportedException) { /* stream is non-seekable; write sequentially instead */ }","preventionTips":["Check CanSeek before assigning Position.","Never reposition the XamlStream writer stream; write sequentially.","Use Seek(SeekOrigin.Begin) instead of the Position setter when supported."],"tags":["wpf","xaml","io","stream"],"backgroundTag":"operation-not-supported","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"}