dotnet/wpf · error · ArgumentException

SR.ReachPackaging_PartFromDifferentContainer

Error message

SR.ReachPackaging_PartFromDifferentContainer

What it means

When registering a starting part, XpsManager verifies that startingPart.Package equals the target package; if the part belongs to a different container, ArgumentException(SR.ReachPackaging_PartFromDifferentContainer) is thrown. A package relationship can only target parts within the same OPC package.

Solutions

  1. Pass the starting part obtained from the same XpsDocument/Package instance you are modifying.
  2. Re-acquire the FixedDocumentSequence part from the target package instead of reusing one from another container.
  3. If you intended to copy content, create an equivalent part inside the target package and use that as the starting part.

Example fix

// before
otherDoc.Package.CreateRelationship(seqFromDocA.Uri, TargetMode.Internal, XpsS0Markup.ReachPackageStartingPartRelationshipType);
// after
var seqFromDocA = docA.GetFixedDocumentSequence();
// use the part belonging to the package being modified:
var seqOfTarget = targetDoc.GetFixedDocumentSequence();
targetDoc.Package.CreateRelationship(seqOfTarget.Uri, TargetMode.Internal, XpsS0Markup.ReachPackageStartingPartRelationshipType);
Defensive patterns

Strategy: validation

Validate before calling

if (startingPart.Package != package) throw new ArgumentException("Part belongs to a different package");

Type guard

bool SamePackage(PackagePart part, Package pkg) => part.Package == pkg;

Try / catch

catch (ArgumentException) { /* wrong container: re-acquire the part from the target package */ }

Prevention

When it happens

Trigger: Calling the starting-part API with an XpsFixedDocumentSequenceReader/part obtained from a different XpsDocument/Package instance than the package being modified — e.g. mixing documents opened from two files or streams.

Common situations: Copying content between two XpsDocuments and passing the source document's sequence part to the target package; holding a cached FixedDocumentSequence reference after opening a new XpsDocument.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/e007778d73e07012. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs:1475

            // won't be removed from the package
            //

            if (package.FileOpenAccess == FileAccess.Read)
            {
                throw new IOException(SR.ReachPackaging_CannotModifyReadOnlyContainer);
            }

            //
            // Throw If the part provided is null
            //
            ArgumentNullException.ThrowIfNull(startingPart);

            //
            // Throw If the part provided is from a different container
            //
            if (startingPart.Package != package)
            {
                throw new ArgumentException(SR.ReachPackaging_PartFromDifferentContainer);
            }

                package.CreateRelationship(startingPart.Uri, TargetMode.Internal, XpsS0Markup.ReachPackageStartingPartRelationshipType);
        }

        #endregion Internal static methods

        #region Public static methods

        /// <summary>
        /// This method generates a relative URI path based on the base URI
        /// and the absolute URI.
        /// </summary>
        /// <param name="baseUri">
        /// The base uri for the part.
        /// </param>
        /// <param name="fileUri">
        /// The absolute path URI to be converted to relative.

View on GitHub (pinned to 81131a70a4)