{"record":{"id":"5232b078ae71761a","repo":"dotnet/wpf","slug":"duplicates-not-allowed-signature-part-already-exists","errorCode":null,"errorMessage":"Duplicates not allowed - signature part already exists.","messagePattern":"Duplicates not allowed - signature part already exists\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/PackageDigitalSignatureManager.cs","lineNumber":463,"sourceCode":"                throw new InvalidOperationException(SR.CannotSignReadOnlyFile);\n\n            VerifySignArguments(parts, certificate, relationshipSelectors, signatureId, signatureObjects, objectReferences);\n\n            // substitute default id if none given\n            if (string.IsNullOrEmpty(signatureId))\n            {\n                signatureId = \"packageSignature\";   // default\n            }\n\n            // Make sure the list reflects what's in the package.\n            // Do this before adding the new signature part because we don't want it included until it\n            // is fully formed (and delaying the add saves us having to remove it in case there is an \n            // error during the Sign call).\n            EnsureSignatures();\n\n            Uri newSignaturePartName = GenerateSignaturePartName();\n            if (_container.PartExists(newSignaturePartName))\n                throw new ArgumentException(SR.DuplicateSignature);\n\n            // Pre-create origin part if it does not already exist.\n            // Do this before signing to allow for signing the package relationship part (because a Relationship\n            // is added from the Package to the Origin part by this call) and the Origin Relationship part in case this is\n            // a Publishing signature and the caller wants the addition of more signatures to break this signature.\n            PackageRelationship relationshipToNewSignature = OriginPart.CreateRelationship(newSignaturePartName, TargetMode.Internal,\n                    _originToSignatureRelationshipType);\n            _container.Flush();     // ensure the origin relationship part is persisted so that any signature will include this newest relationship\n\n            VerifyPartsExist(parts);\n\n            // sign the data and optionally embed the certificate\n            bool embedCertificateInSignaturePart = (_certificateEmbeddingOption == CertificateEmbeddingOption.InSignaturePart);\n\n            // convert cert to version2 - more functionality\n            if (certificate is not X509Certificate2 exSigner)\n                exSigner = new X509Certificate2(certificate.Handle);\n","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/PackageDigitalSignatureManager.cs#L445-L481","documentation":"Before writing a new signature, Sign generates a signature part name and throws ArgumentException (SR.DuplicateSignature) if a part with that URI already exists in the package, to avoid overwriting or duplicating an existing signature part.","triggerScenarios":"A previous Sign call failed midway and left a pre-created signature/origin part behind; the generated signature part name collides with an existing part; signing the same package twice with deterministic naming where cleanup never happened.","commonSituations":"Retrying a failed sign operation on the same package without removing leftover parts; a crashed process leaving half-written signature parts; manually renaming or copying signature parts in the package.","solutions":["Remove leftover signature parts (via DeletePart) from the failed attempt before re-signing","Inspect and clean the package's /_rels/DigitalSignatures and signature origin parts after a failed sign","Recreate the package from source if its signature parts are in a corrupted state","Catch ArgumentException and offer the user a 'repair package then re-sign' path"],"exampleFix":"// before\nmgr.Sign(parts, cert); // retry after earlier failure -> duplicate signature part\n// after\nvar existing = mgr.Signatures.Select(s => s.SignaturePart.Uri).ToList();\nforeach (var uri in existing)\n    pkg.DeletePart(uri);\nmgr.Sign(parts, cert);","handlingStrategy":"try-catch","validationCode":"Uri next = /* generated name */;\nif (package.PartExists(next))\n    package.DeletePart(next); // clean leftover from failed attempt","typeGuard":null,"tryCatchPattern":"try { manager.Sign(parts, cert); }\ncatch (ArgumentException ex) { log.Error(\"Duplicate signature part\", ex); /* cleanup and retry */ }","preventionTips":["After a failed sign, delete orphaned signature/origin parts before retrying","Avoid mutating signature parts manually inside the package","Serialize signing operations so concurrent signs cannot collide on part names"],"tags":["wpf","packaging","argumentexception","duplicate-part"],"backgroundTag":"file-already-exists","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"}