{"record":{"id":"cd5bf5710eaa342b","repo":"dotnet/wpf","slug":"only-writers-can-call-this-method","errorCode":null,"errorMessage":"Only writers can call this method.","messagePattern":"Only writers can call this method\\.","errorType":"exception","errorClass":"XpsPackagingException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs","lineNumber":295,"sourceCode":"\n        #endregion Public properties\n\n        #region Public methods\n        /// <summary>\n        /// Generate a unique Part for the content type and add it to the package.\n        /// Adding to any relationships or selector/sequence markup is not done.\n        /// </summary>\n        public\n        PackagePart\n        GeneratePart(\n            ContentType contentType,\n            Uri      \tpartUri\n            )\n       {\n            ObjectDisposedException.ThrowIf(_metroPackage is null, typeof(XpsManager));\n            if (!IsWriter)\n            {\n                throw new XpsPackagingException(SR.ReachPackaging_OnlyWriters);\n            }\n            ArgumentNullException.ThrowIfNull(contentType);\n            if (0 == contentType.ToString().Length)\n            {\n                throw new ArgumentException(SR.Format(SR.ReachPackaging_InvalidContentType, contentType), nameof(contentType));\n            }\n            \n            //Do not compress image Content Types\n            CompressionOption compressionOption = _compressionOption;\n\n            if (contentType.AreTypeAndSubTypeEqual(XpsS0Markup.JpgContentType) ||\n                contentType.AreTypeAndSubTypeEqual(XpsS0Markup.PngContentType) ||\n                contentType.AreTypeAndSubTypeEqual(XpsS0Markup.TifContentType) ||\n                contentType.AreTypeAndSubTypeEqual(XpsS0Markup.WdpContentType))\n            {\n                compressionOption = CompressionOption.NotCompressed;\n            }\n","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs#L277-L313","documentation":"XpsManager.GeneratePart refuses to run when the XpsManager was not opened in writer mode. Only the instance designated as the package writer may add parts to the XPS package; all other instances (readers) throw XpsPackagingException with SR.ReachPackaging_OnlyWriters. This enforces single-writer access to the underlying Package.","triggerScenarios":"Calling GeneratePart on an XpsManager whose IsWriter property is false — i.e. an XpsDocument opened with the writer parameter false or obtained for read-only access — while trying to add a part with a given partUri and contentType.","commonSituations":"Opening an XpsDocument for read access (e.g. to inspect fixed documents) and then attempting to add resources or parts; sharing one package between a reader and expecting it to also write; opening the document from a read-only file/stream so the writer flag could not be set.","solutions":["Open the XpsDocument/XpsManager in write mode so IsWriter is true (pass true for the writer/editable argument when constructing the XpsDocument).","Check xpsManager.IsWriter before calling GeneratePart and route write operations to the instance that owns write access.","Ensure the underlying file/stream is writable; open a new writable stream if the source was read-only.","Verify only one writer instance exists and write calls are not accidentally made on a reader obtained from GetXpsDocument/Packaging conversions."],"exampleFix":"// before\nvar doc = new XpsDocument(pkgPath, FileAccess.Read);\nvar part = xpsManager.GeneratePart(partUri, contentType, compressionOption);\n// after\nvar doc = new XpsDocument(pkgPath, FileAccess.ReadWrite);\nif (!xpsManager.IsWriter) throw new InvalidOperationException(\"This XpsManager is read-only\");\nvar part = xpsManager.GeneratePart(partUri, contentType, compressionOption);","handlingStrategy":"validation","validationCode":"if (xpsManager == null || xpsManager.IsDisposed) throw new ObjectDisposedException(nameof(xpsManager));\nif (!xpsManager.IsWriter) throw new InvalidOperationException(\"GeneratePart requires an XpsManager opened in writer mode.\");","typeGuard":"bool CanWrite(XpsManager m) => m is { IsWriter: true };","tryCatchPattern":"try { var part = xpsManager.GeneratePart(partUri, contentType, compressionOption); }\ncatch (XpsPackagingException ex) when (ex.Message.Contains(\"writers\")) { /* route to writer instance or reopen writable */ }","preventionTips":["Always open the XpsDocument with FileAccess.ReadWrite or a write stream when mutations are planned.","Expose a single writer instance and perform all part additions through it.","Assert IsWriter at the start of write-path helper methods."],"tags":["wpf","xps","packaging","write-access"],"backgroundTag":"permission-denied","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"}