{"record":{"id":"6b936e1ee399c0d6","repo":"dotnet/wpf","slug":"stream-does-not-support-setlength-netstream","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/PresentationCore/MS/internal/IO/Packaging/NetStream.cs","lineNumber":301,"sourceCode":"                    throw new ArgumentException(SR.SeekNegative);\n\n#if DEBUG\n                if (System.IO.Packaging.PackWebRequestFactory._traceSwitch.Enabled)\n                    System.Diagnostics.Trace.TraceInformation(\"NetStream.set_Position() pos:{0}\", value);\n#endif\n\n                _position = value;\n            }\n        }\n\n\n        /// <summary>\n        /// SetLength\n        /// </summary>\n        /// <exception cref=\"NotSupportedException\">not supported</exception>\n        public override void SetLength(long newLength)\n        {\n            throw new NotSupportedException(SR.SetLengthNotSupported);\n        }\n\n\n        /// <summary>\n        /// Write\n        /// </summary>\n        /// <exception cref=\"NotSupportedException\">not supported</exception>\n        public override void Write(byte[] buf, int offset, int count)\n        {\n            throw new NotSupportedException(SR.WriteNotSupported);\n        }\n\n\n        /// <summary>\n        /// Length\n        /// </summary>\n        public override long Length\n        {","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/NetStream.cs#L283-L319","documentation":"NetStream is a read-only stream over a network/resource stream for package parts; it deliberately does not implement resizing, so SetLength always throws NotSupportedException (SR.SetLengthNotSupported). This library throws it because the underlying transport provides no truncation/extension capability.","triggerScenarios":"Calling SetLength(long) on any NetStream instance — unconditionally thrown for every value of newLength.","commonSituations":"Generic stream-copy or resize helpers that call SetLength on the destination stream; shared code paths written for MemoryStream/FileStream reused against package-part streams.","solutions":["Copy the part into a writable MemoryStream or FileStream and resize that instead","Gate the code path with CanWrite/CanSeek checks and skip resizing for read-only streams","Rewrite the package part with the desired size via Packaging APIs rather than resizing in place"],"exampleFix":"// before\ndestStream.SetLength(totalSize);\n// after\nif (destStream.CanWrite)\n    CopyToResizableStream(destStream, totalSize); // copy into MemoryStream/FileStream\nelse\n    throw new NotSupportedException(\"Read-only NetStream cannot be resized\");","handlingStrategy":"type-guard","validationCode":"if (!stream.CanWrite || !stream.CanSeek)\n    throw new NotSupportedException(\"SetLength requires a writable, seekable stream; copy to MemoryStream first\");","typeGuard":"static bool SupportsSetLength(Stream s) => s.CanWrite && s.CanSeek && s is not System.Net.Sockets.NetworkStream;","tryCatchPattern":"try { stream.SetLength(size); } catch (NotSupportedException) { using var ms = new MemoryStream(); stream.CopyTo(ms); ms.SetLength(size); /* use ms */ }","preventionTips":["Check Stream.CanWrite before resize/write operations","Treat package-part streams as read-only; copy to MemoryStream for mutation","Avoid generic helpers that assume all Streams support SetLength"],"tags":["packaging","stream","not-supported","read-only"],"backgroundTag":"unsupported-operation","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"}