dotnet/wpf · error · XpsSerializationException

SR.Format(SR.MustBeOfType…

Error message

SR.Format(SR.MustBeOfType, "serializableObjectContext.TargetObject", typeof(System.Collections.Generic.IEnumerable<DocumentReference>))

What it means

The asynchronous variant (ReachDocumentReferenceCollectionSerializerAsync.PersistObjectData) throws the same XpsSerializationException as its synchronous twin when the context's TargetObject is not an IEnumerable<DocumentReference>. The async serializer's contract requires a DocumentReference collection; any other target object fails the 'as' cast and aborts with the MustBeOfType message.

Solutions

  1. Pass an IEnumerable<DocumentReference> (e.g., FixedDocumentSequence.References) as the serialization target for the async collection serializer.
  2. Correct the serializer registration so the async collection serializer is only selected for DocumentReference collections.
  3. Add an upfront type check on the target object before starting async serialization to fail fast with a clearer error.

Example fix

// before
asyncSerializer.SerializeObject(singleDocument);

// after
if (target is IEnumerable<DocumentReference> refs)
    asyncSerializer.SerializeObject(refs);
Defensive patterns

Strategy: type-guard

Validate before calling

if (serializableObjectContext.TargetObject is not IEnumerable<DocumentReference>)
    throw new ArgumentException("Async serializer expects IEnumerable<DocumentReference>");

Type guard

bool IsAsyncDocRefCollection(SerializableObjectContext ctx) => ctx.TargetObject is IEnumerable<DocumentReference>;

Try / catch

try { await asyncSerializeAsync(obj); }
catch (XpsSerializationException ex) when (ex.Message.Contains("MustBeOfType")) {
    // target type mismatch in async path
}

Prevention

When it happens

Trigger: Async XPS serialization (XpsSerializationManagerAsync) invoked on an object whose TargetObject is not IEnumerable<DocumentReference> — e.g., a single FixedDocument, FixedDocumentSequence, or mistyped collection — routed to this serializer by the registration table.

Common situations: Using async serialization of an XPS document with the wrong root object; custom serializer registration mapping DocumentReference collections to the wrong type; code paths shared with sync serialization where the object type differs.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachDocumentReferenceCollectionSerializerAsync.cs:83

        /// <summary>
        /// 
        /// </summary>
        internal
        override
        void
        PersistObjectData(
            SerializableObjectContext   serializableObjectContext
            )
        {
            ArgumentNullException.ThrowIfNull(serializableObjectContext);

            // get DocumentReferenceCollection
            System.Collections.Generic.IEnumerable<DocumentReference> enumerableObject = serializableObjectContext.TargetObject as System.Collections.Generic.IEnumerable<DocumentReference>;

            if (enumerableObject == null)
            {
                throw new XpsSerializationException(SR.Format(SR.MustBeOfType, "serializableObjectContext.TargetObject", typeof(System.Collections.Generic.IEnumerable<DocumentReference>)));
            }

            SerializeDocumentReferences(serializableObjectContext);
        }

        internal
        override
        void
        EndPersistObjectData(
            )
        {
            //
            // do nothing in this stage
            //
        }

        /// <summary>
        /// This is being called to serialize the DocumentReference items

View on GitHub (pinned to 81131a70a4)