{"record":{"id":"700b72bb4c31c384","repo":"dotnet/wpf","slug":"can-t-seek-on-basestream","errorCode":null,"errorMessage":"can’t seek on baseStream","messagePattern":"can’t seek on baseStream","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs","lineNumber":47,"sourceCode":"\n        /// <summary>\n        /// Constructor that limits the bytes that can be written to or read form\n        /// </summary>\n        /// <param name=\"baseStream\"></param>\n        /// <param name=\"offset\"></param>\n        /// <param name=\"length\"></param>\n        public SharedStream(Stream baseStream, long offset, long length)\n        {\n            ArgumentNullException.ThrowIfNull(baseStream);\n\n            Initialize(baseStream, offset, length);\n        }\n\n        private void Initialize(Stream baseStream, long offset, long length)\n        {\n            if (!baseStream.CanSeek)\n            {\n                throw new ArgumentException(\"can\\u2019t seek on baseStream\");\n            }\n\n            ArgumentOutOfRangeException.ThrowIfNegative(offset);\n\n            ArgumentOutOfRangeException.ThrowIfNegative(length);\n\n            SharedStream subStream = baseStream as SharedStream;\n            if (subStream != null)\n            {\n                _baseStream = subStream.BaseStream;\n                _offset = offset + subStream._offset;\n                _length = length;\n                _refCount = subStream._refCount;\n                _refCount.Value++;\n            }\n            else\n            {\n                _baseStream = baseStream;","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs#L29-L65","documentation":"SharedStream wraps an underlying stream at a fixed offset/length window, and that requires the base stream to support seeking (CanSeek). Initialize throws ArgumentException \"can't seek on baseStream\" when handed a non-seekable stream such as a network or pipe stream.","triggerScenarios":"Constructing SharedStream (via Initialize) with a Stream whose CanSeek is false — e.g. NetworkStream, stdout/stdin streams, or an unbuffered deflate/http response stream.","commonSituations":"Loading BAML from a web response or network stream without buffering; passing Console.OpenStandardInput/Output; wrapping a stream obtained from a streaming HTTP download of a XAP/resource.","solutions":["Wrap the non-seekable source in a seekable buffer first: copy into MemoryStream (or FileStream) and pass that to SharedStream.","Use File.ReadAllBytes/FileStream when the BAML originates from disk.","For HTTP sources, download the full response to memory before constructing the stream.","If you own the API boundary, pre-check baseStream.CanSeek and fail fast with a clear message."],"exampleFix":"// before\nusing var resp = await http.SendAsync(req);\nvar shared = new SharedStream(resp.Content.ReadAsStream(), 0, length); // not seekable\n// after\nusing var resp = await http.SendAsync(req);\nusing var ms = new MemoryStream();\nawait resp.Content.CopyToAsync(ms);\nms.Position = 0;\nvar shared = new SharedStream(ms, 0, ms.Length);","handlingStrategy":"type-guard","validationCode":"// Pre-check seekability before constructing SharedStream\nif (!source.CanSeek)\n{\n    var buffered = new MemoryStream();\n    source.CopyTo(buffered);\n    buffered.Position = 0;\n    source = buffered;\n}","typeGuard":"static bool IsSeekable(Stream s) => s is { CanSeek: true };","tryCatchPattern":"try\n{\n    var shared = new SharedStream(source, offset, length);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"can’t seek\"))\n{\n    // buffer the source into a seekable stream and retry\n    throw new InvalidOperationException(\"Buffer the non-seekable stream into MemoryStream first.\", ex);\n}","preventionTips":["Always check Stream.CanSeek before slicing a stream","Buffer network/HTTP streams fully before parsing","Prefer FileStream/MemoryStream as SharedStream inputs","Never pass console/pipe/network streams directly"],"tags":["io","stream","seek","wpf"],"backgroundTag":"incompatible-source-type","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"}