{"record":{"id":"49dd856f0b60657d","repo":"dotnet/wpf","slug":"sr-documentstreammustbetemporary","errorCode":null,"errorMessage":"SR.DocumentStreamMustBeTemporary","messagePattern":"SR\\.DocumentStreamMustBeTemporary","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/DocumentStream.cs","lineNumber":503,"sourceCode":"    /// <exception cref=\"System.InvalidOperationException\"/>\n    /// <returns>False if the operation failed.</returns>\n    /// <remarks>\n    /// This method is inplace to work around issues with re-publishing\n    /// XpsDocuments into the same file.  The intended use for the method is \n    /// to logically allow in place editing for the user.\n    /// \n    /// After use this object is unusable and should be disposed as the\n    /// temporary file is gone; it has become the original. In the event of an\n    /// error while swapping the file, the file no longer becomes the original,\n    /// but this object still becomes unusable.\n    /// </remarks>\n    internal bool SwapWithOriginal()\n    {\n        bool success = false;\n\n        if (_original == null)\n        {\n            throw new InvalidOperationException(\n                SR.DocumentStreamMustBeTemporary);\n        }\n        if (_original._xpsFileToken == null)\n        {\n            throw new InvalidOperationException(\n                SR.DocumentStreamMustBeFileSource);\n        }\n        if (_xpsFileToken == null)\n        {\n            throw new InvalidOperationException(\n                SR.DocumentStreamMustBeFileSource);\n        }\n\n        Trace.SafeWrite(\n            Trace.File,\n            \"Begining file swap between {0} and {1}.\",\n            _xpsFileToken.Location,\n            _original._xpsFileToken.Location);","sourceCodeStart":485,"sourceCodeEnd":521,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/DocumentStream.cs#L485-L521","documentation":"DocumentStream.SwapWithOriginal throws InvalidOperationException if the stream's _original reference is null, meaning this stream is not a temporary copy of another stream. Only temporary streams (created from an original) can be swapped back, so the operation is invalid on a directly opened stream.","triggerScenarios":"Calling SwapWithOriginal() on a DocumentStream that was constructed directly from a file/stream rather than as a temporary working copy created via MakeTempFile/Copy.","commonSituations":"Developers calling internal swap logic on a plainly opened XPS document, or calling SwapWithOriginal twice (the first swap clears the temporary relationship).","solutions":["Only call SwapWithOriginal on streams created as temporary copies of an original DocumentStream.","Check that the stream is temporary before swapping; skip the call otherwise.","Do not call SwapWithOriginal more than once on the same stream."],"exampleFix":"// before\ndocStream.SwapWithOriginal();\n// after\nif (docStream.IsTemporary) docStream.SwapWithOriginal(); // only when backed by an original","handlingStrategy":"validation","validationCode":"bool canSwap = docStream.IsTemporary && docStream.Original != null;","typeGuard":null,"tryCatchPattern":"try { docStream.SwapWithOriginal(); }\ncatch (InvalidOperationException) { /* not a temporary stream; use alternate save path */ }","preventionTips":["Only invoke swap logic on streams created via MakeTempFile/Copy.","Never call SwapWithOriginal twice on the same stream."],"tags":["xps","invalid-operation","stream","state"],"backgroundTag":"invalid-state-transition","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"}