dotnet/wpf · error · InvalidOperationException

SR.NestedBeginInitNotSupported

Error message

SR.NestedBeginInitNotSupported

What it means

FrameworkContentElement.BeginInit throws InvalidOperationException if called while an initialization is already pending (nested BeginInit on the same instance). The element tracks an InitPending internal flag; BeginInit must always be paired one-to-one with EndInit, and re-entrant calls would corrupt that pairing.

Solutions

  1. Check ReadLocalValue / track your own flag and only call BeginInit if not already pending
  2. Ensure every BeginInit has a matching EndInit before calling BeginInit again (use try/finally)
  3. Remove manual BeginInit calls when the XAML loader already performs initialization for the element

Example fix

// before
el.BeginInit();
Configure(el);
el.BeginInit(); // throws: nested
el.EndInit(); el.EndInit();

// after
el.BeginInit();
try { Configure(el); }
finally { el.EndInit(); }
Defensive patterns

Strategy: validation

Validate before calling

if (initPending) // track with your own bool or a HashSet<object>
    return;
el.BeginInit();
try { Configure(el); }
finally { el.EndInit(); initPending = false; }

Type guard

bool IsBeginInitAllowed => !initPending;

Try / catch

try { el.BeginInit(); Configure(el); }
catch (InvalidOperationException ex) { log.Error("Nested BeginInit", ex); }
finally { try { el.EndInit(); } catch { } }

Prevention

When it happens

Trigger: Calling BeginInit() twice on the same FrameworkContentElement without an intervening EndInit(), or calling it manually while a XAML/BAML loader is already initializing the element.

Common situations: Custom factory/serialization code that calls BeginInit defensively while the XAML loader is also initializing the same object; recursive construction of the same element.

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/b66f04edf2b58030. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkContentElement.cs:1529

                {
                    throw new InvalidOperationException(SR.Illegal_InheritanceBehaviorSettor);
                }
            }
        }

        #endregion LogicalTree

        #region ISupportInitialize

        /// <summary>
        ///     Initialization of this element is about to begin
        /// </summary>
        public virtual void BeginInit()
        {
            // Nested BeginInits on the same instance aren't permitted
            if (ReadInternalFlag(InternalFlags.InitPending))
            {
                throw new InvalidOperationException(SR.NestedBeginInitNotSupported);
            }

            // Mark the element as pending initialization
            WriteInternalFlag(InternalFlags.InitPending, true);
        }

        /// <summary>
        ///     Initialization of this element has completed
        /// </summary>
        public virtual void EndInit()
        {
            // Every EndInit must be preceeded by a BeginInit
            if (!ReadInternalFlag(InternalFlags.InitPending))
            {
                throw new InvalidOperationException(SR.EndInitWithoutBeginInitNotSupported);
            }

            // Reset the pending flag

View on GitHub (pinned to 81131a70a4)