{"record":{"id":"e23428ecb91754a1","repo":"dotnet/wpf","slug":"sr-effect-shaderseekablestream","errorCode":null,"errorMessage":"SR.Effect_ShaderSeekableStream","messagePattern":"SR\\.Effect_ShaderSeekableStream","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/PixelShader.cs","lineNumber":123,"sourceCode":"            finally\n            {\n                stream?.Dispose();\n            }\n        }\n\n        /// <summary>\n        /// Reads the byte code for the pixel shader into a local byte array. If the stream is null, the byte array\n        /// will be empty (length 0). The compositor will use an identity shader.\n        /// </summary>\n        private void LoadPixelShaderFromStreamIntoMemory(Stream source) \n        {\n            _shaderBytecode = null;\n\n            if (source != null)\n            {\n                if (!source.CanSeek)\n                {\n                    throw new InvalidOperationException(SR.Effect_ShaderSeekableStream);\n                }\n\n                int len = (int)source.Length;  // only works on seekable streams.\n\n                if (len % sizeof(int) != 0)\n                {\n                    throw new InvalidOperationException(SR.Effect_ShaderBytecodeSize);\n                }\n                \n                BinaryReader br = new BinaryReader(source);\n                _shaderBytecode = new byte[len];\n                int lengthRead = br.Read(_shaderBytecode, 0, (int)len);\n\n                //\n                // The first 4 bytes contain version info in the form of\n                // [Minor][Major][xx][xx]\n                //\n                if (_shaderBytecode != null && _shaderBytecode.Length > 3)","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/PixelShader.cs#L105-L141","documentation":"PixelShader.SetStreamSource (or the URI load path) received a stream whose CanSeek is false. The loader needs source.Length to size the bytecode buffer, which only works on seekable streams, so it throws InvalidOperationException with SR.Effect_ShaderSeekableStream.","triggerScenarios":"Calling shader.SetStreamSource(networkStream / non-seekable decompression stream); passing a forward-only stream (e.g. raw HttpResponse stream, streaming pipeline) as shader bytecode source.","commonSituations":"Downloading .ps bytecode directly from HTTP and passing the response stream; wrapping shader bytes in a CryptoStream or GZipStream chain that lost seekability; memory pressure code that swapped MemoryStream for a pipe.","solutions":["Buffer the stream into a MemoryStream first: var ms = new MemoryStream(); source.CopyTo(ms); ms.Position = 0; shader.SetStreamSource(ms)","Read the bytes and load from a seekable wrapper (new MemoryStream(byteArray))","Prefer loading bytecode from an embedded resource stream that is seekable","Avoid removing MemoryStream from the shader-loading path; the loader requires Length"],"exampleFix":"// before\nshader.SetStreamSource(responseStream); // network stream, not seekable: throws\n\n// after\nvar ms = new MemoryStream();\nresponseStream.CopyTo(ms);\nms.Position = 0;\nshader.SetStreamSource(ms); // seekable","handlingStrategy":"validation","validationCode":"if (source == null) throw new ArgumentNullException(nameof(source));\nif (!source.CanSeek)\n{\n    var buffered = new MemoryStream();\n    source.CopyTo(buffered);\n    buffered.Position = 0;\n    source = buffered;\n}\nshader.SetStreamSource(source);","typeGuard":"static bool IsSeekable(Stream s) => s != null && s.CanSeek;","tryCatchPattern":"try { shader.SetStreamSource(stream); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"seek\"))\n{\n    var ms = new MemoryStream();\n    stream.CopyTo(ms);\n    ms.Position = 0;\n    shader.SetStreamSource(ms);\n}","preventionTips":["Always buffer non-seekable streams into MemoryStream before SetStreamSource","Check Stream.CanSeek before handing a stream to any WPF loader","Load shader bytecode from embedded resources (seekable) when possible"],"tags":["wpf","pixelshader","stream","seekable"],"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"}