{"record":{"id":"6d58b63956268578","repo":"dotnet/wpf","slug":"notsupportedexception","errorCode":null,"errorMessage":"NotSupportedException","messagePattern":"NotSupportedException","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft/Build/Tasks/Windows/ResourcesGenerator.cs","lineNumber":93,"sourceCode":"            {\n                get { return SourceStream.Position; }\n                set { SourceStream.Position = value; }\n            }\n\n            public override int Read(byte[] buffer, int offset, int count)\n            {\n                return SourceStream.Read(buffer, offset, count);\n            }\n\n            public override long Seek(long offset, SeekOrigin origin)\n            {\n                return SourceStream.Seek(offset, origin);\n            }\n\n            public override void SetLength(long value)\n            {\n                // This is backed by a readonly file.\n                throw new NotSupportedException();\n            }\n\n            public override void Write(byte[] buffer, int offset, int count)\n            {\n                // This is backed by a readonly file.\n                throw new NotSupportedException();\n            }\n\n            protected override void Dispose(bool disposing)\n            {\n                if (disposing)\n                {\n                    _sourceStream?.Dispose();\n                    _sourceStream = null;\n                }\n            }\n        }\n        ","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft/Build/Tasks/Windows/ResourcesGenerator.cs#L75-L111","documentation":"LazyFileStream in ResourcesGenerator is a read-only stream backed by a resource file opened with FileAccess.Read. SetLength is not a meaningful operation on a read-only file, so the override unconditionally throws NotSupportedException.","triggerScenarios":"Any call to SetLength(long) on the LazyFileStream instance returned by ResourcesGenerator (e.g. code treating the resource stream as writable, or a component like a serializer that calls SetLength before writing).","commonSituations":"Passing the read-only resource stream to APIs that expect a writable Stream (e.g. writers that truncate via SetLength), instead of supplying a MemoryStream or file-backed writable stream.","solutions":["Do not call SetLength on this stream; use CanWrite/CanSeek checks before invoking write APIs.","Copy the resource stream into a writable MemoryStream or FileStream and pass that to the write-oriented API.","Wrap the stream in a ReadOnlyStream adapter that reports CanWrite=false so consumers never attempt writes."],"exampleFix":"// before\nwriterStream.SetLength(0);\n// after\nusing (var ms = new MemoryStream())\n{\n    resourceStream.CopyTo(ms);\n    ms.SetLength(0); // operate on writable stream\n}","handlingStrategy":"type-guard","validationCode":"if (!stream.CanWrite)\n    throw new InvalidOperationException(\"Stream is read-only; SetLength is not supported\");","typeGuard":"bool IsWritable(Stream s) => s != null && s.CanWrite;","tryCatchPattern":"try { stream.SetLength(0); }\ncatch (NotSupportedException) { /* copy to writable stream instead */ }","preventionTips":["Check CanWrite/CanSeek before write-style operations on streams","Wrap read-only streams so consumers see CanWrite=false","Copy to MemoryStream when mutation is needed"],"tags":["read-only-stream","unsupported-operation","resources"],"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"}