{"record":{"id":"618424ccf148ea68","repo":"dotnet/wpf","slug":"notsupportedexception-sharedstream","errorCode":null,"errorMessage":"NotSupportedException","messagePattern":"NotSupportedException","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs","lineNumber":251,"sourceCode":"                default:\n                    throw new InvalidEnumArgumentException(nameof(origin), (int)origin, typeof(SeekOrigin));\n            }\n\n            if (newPosition < 0 || newPosition >= _length)\n            {\n                throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);\n            }\n\n            CheckDisposed();\n\n            _position = newPosition;\n\n            return _position;\n        }\n\n        public override void SetLength(long value)\n        {\n            throw new NotSupportedException();\n        }\n\n        protected override void Dispose(bool disposing)\n        {\n            if (disposing)\n            {\n                if (_baseStream != null)\n                {\n                    _refCount.Value--;\n                    if (_refCount.Value < 1)\n                    {\n                        _baseStream.Close();\n                    }\n                    _refCount = null;\n                    _baseStream = null;\n                }\n            }\n","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs#L233-L269","documentation":"SharedStream is a read-only stream over a shared byte range, so SetLength(long) unconditionally throws NotSupportedException at any call. Any attempt to resize the stream (directly or via Stream APIs such as GetBuffer-less writers) hits this sentinel error; callers must not attempt to modify the stream's length.","triggerScenarios":"Calling sharedStream.SetLength(value) directly, or via any API that resizes the stream (e.g. passing it to a writer that calls SetLength, like StreamWriter with truncate semantics or serialization code that grows the stream).","commonSituations":"Trying to append or truncate BAML in place through the shared window; passing the stream to code that assumes a fully writable FileStream-like stream.","solutions":["Don't call SetLength on SharedStream — write to the underlying base stream instead if mutation is required.","Copy the window into a writable MemoryStream/FileStream and operate on that copy.","Restructure code so BAML is treated as immutable input (read-only parsing).","If you need a writable stream, construct your own FileStream rather than slicing an existing one."],"exampleFix":"// before\nsharedStream.SetLength(0); // NotSupportedException\n// after\nvar buffer = new MemoryStream();\nsharedStream.CopyTo(buffer);\nbuffer.SetLength(0);","handlingStrategy":"try-catch","validationCode":"// Check writability/resize capability before resizing attempts\nif (stream is SharedStream)\n    throw new InvalidOperationException(\"SharedStream is a read-only window; copy to a writable stream first.\");","typeGuard":"static bool CanResize(Stream s) => s is not SharedStream && s.CanWrite;","tryCatchPattern":"try\n{\n    stream.SetLength(0);\n}\ncatch (NotSupportedException)\n{\n    // fall back: copy to a writable stream and resize the copy\n    var copy = new MemoryStream();\n    stream.Position = 0;\n    stream.CopyTo(copy);\n    copy.SetLength(0);\n}","preventionTips":["Treat SharedStream as immutable input — parse, never mutate","Copy to MemoryStream/FileStream when modification is needed","Check CanWrite before write-oriented APIs on streams","Document stream ownership so writers don't receive read-only slices"],"tags":["io","stream","read-only","not-supported"],"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-22T01:17:13.364Z"}