dotnet/wpf · error · ArgumentException

SR.MustBeOfType

Error message

SR.MustBeOfType

What it means

Async variant of error 3750: ReachVisualSerializerAsync.SerializeObject throws ArgumentException (SR.MustBeOfType) when serializedObject is not a Visual. The async serialization manager additionally expects to cast SerializationManager to IXpsSerializationManagerAsync, so the argument must be a Visual for the asynchronous page-writing pipeline to proceed.

Solutions

  1. Pass a Visual-derived object to SerializeObject
  2. Check for null/incorrect type before invoking the async serializer
  3. Use IXpsSerializationManagerAsync with a compatible packaging policy so pages can be acquired

Example fix

// before
asyncSerializer.SerializeObject(model);
// after
if (model is Visual visual) asyncSerializer.SerializeObject(visual);
else throw new ArgumentException("serializedObject must be a Visual", nameof(model));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(obj is Visual)) return; // skip or throw before async work
var visual = (Visual)obj;

Type guard

bool IsVisual(object o) => o is Visual;

Try / catch

try { await SerializeAsync(obj); }
catch (ArgumentException ex) when (ex.ParamName == "serializedObject") { /* non-Visual */ }

Prevention

When it happens

Trigger: Calling the async serializer's SerializeObject with null or a non-Visual serializedObject during asynchronous XPS document production.

Common situations: Async XPS printing/print-ticket workflows where the caller passes data objects instead of visuals; null results from failed visual creation; mixing sync and async serializer APIs.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachVisualSerializerAsync.cs:76

                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public
        override
        void
        SerializeObject(
            object serializedObject
            )
        {
            Visual v = serializedObject as Visual;

            if (v == null)
            {
                throw new ArgumentException(SR.Format(SR.MustBeOfType, "serializedObject", typeof(Visual)));
            }

            IXpsSerializationManagerAsync manager = (IXpsSerializationManagerAsync)SerializationManager;

            XmlWriter pageWriter  = ((PackageSerializationManager)manager).
                                    PackagingPolicy.AcquireXmlWriterForPage();

            XmlWriter resWriter = ((PackageSerializationManager)manager).
                                    PackagingPolicy.AcquireXmlWriterForResourceDictionary();

            SerializeTree(v, resWriter, pageWriter);
        }

        /// <summary>
        /// 
        /// </summary>
        internal
        override

View on GitHub (pinned to 81131a70a4)