dotnet/wpf · error · InvalidOperationException

SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState

Error message

SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState

What it means

WriteObject throws InvalidOperationException when an object is being written 'from member' (GetObject semantics) but the current namespace-scope frame is not empty, meaning the writer is not in the plain state expected for that operation. Writing an object via a member reference is only valid when no attribute/namespace data is pending on the current frame.

Solutions

  1. Emit WriteNamespace/attribute nodes only after the object-from-member write completes.
  2. Use WriteStartObject instead of WriteObject(isObjectFromMember:true) when the object has its own namespace declarations.
  3. Check the current frame state with the writer's state machine before calling WriteObject.
  4. Catch InvalidOperationException and log/repair the node stream ordering.

Example fix

// before
writer.WriteNamespace(ns); // attaches to current frame
writer.WriteObject(type, isObjectFromMember: true); // throws
// after
writer.WriteObject(type, isObjectFromMember: true);
writer.WriteNamespace(ns);
Defensive patterns

Strategy: validation

Validate before calling

if (isObjectFromMember && hasPendingNamespaces) throw new InvalidOperationException("WriteNamespace must follow WriteObject(isObjectFromMember:true)");

Try / catch

try { writer.WriteObject(type, isObjectFromMember: true); } catch (InvalidOperationException ex) when (ex.Message.Contains("WriteObject")) { /* reorder node stream */ }

Prevention

When it happens

Trigger: Calling WriteObject(type, isObjectFromMember: true) while namespaceScopes.Peek() is non-empty — e.g. directives or namespace declarations were recorded on the frame before the GetObject-style object write.

Common situations: Custom node streams that emit namespace declarations on a GetObject node, writers forwarding nodes from XamlReader with x:Shared/x:Key attached in the wrong position, incorrect ordering of WriteNamespace before WriteObject(isObjectFromMember).

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:1387

            public override void WriteObject(XamlXmlWriter writer, XamlType type, bool isObjectFromMember)
            {
                Debug.Assert(writer.namespaceScopes.Count > 0);
                if (writer.namespaceScopes.Peek().AllocatingNodeType != XamlNodeType.StartObject &&
                    writer.namespaceScopes.Peek().AllocatingNodeType != XamlNodeType.GetObject)
                {
                    writer.namespaceScopes.Push(new Frame { AllocatingNodeType = isObjectFromMember ? XamlNodeType.GetObject : XamlNodeType.StartObject });
                }

                writer.namespaceScopes.Peek().Type = type;
                writer.namespaceScopes.Peek().IsObjectFromMember = isObjectFromMember;

                writer.isFirstElementOfWhitespaceSignificantCollection = false;

                if (isObjectFromMember)
                {
                    if (!writer.namespaceScopes.Peek().IsEmpty())
                    {
                        throw new InvalidOperationException(SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState);
                    }

                    Frame tempFrame = writer.namespaceScopes.Pop();
                    Frame frame = writer.namespaceScopes.Peek();
                    writer.namespaceScopes.Push(tempFrame);

                    if (frame.AllocatingNodeType == XamlNodeType.StartMember)
                    {
                        XamlType memberType = frame.Member.Type;
                        if (memberType is not null && !memberType.IsCollection && !memberType.IsDictionary)
                        {
                            throw new InvalidOperationException(SR.XamlXmlWriterIsObjectFromMemberSetForArraysOrNonCollections);
                        }
                    }

                    writer.currentState = InRecord.State;
                }
                else

View on GitHub (pinned to 81131a70a4)