dotnet/wpf · error · InvalidOperationException

SR.EndInitWithoutBeginInitNotSupported

Error message

SR.EndInitWithoutBeginInitNotSupported

What it means

FrameworkContentElement.EndInit throws InvalidOperationException if called without a preceding BeginInit. EndInit completes the initialization phase, marks the element as initialized, and raises the Initialized event; calling it with no pending init would produce spurious Initialized events and break the BeginInit/EndInit contract.

Solutions

  1. Pair BeginInit/EndInit one-to-one using try/finally so EndInit runs exactly once per BeginInit
  2. Track the pending state yourself (or query element initialization state) before calling EndInit
  3. Remove redundant EndInit calls when the framework/XAML loader already finalizes the element

Example fix

// before
el.EndInit();
Configure(el);
el.EndInit(); // second call throws

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

Strategy: validation

Validate before calling

if (!initPending) // must have called BeginInit first
    el.BeginInit();
try { Configure(el); }
finally { el.EndInit(); }

Type guard

bool IsEndInitAllowed => initPending;

Try / catch

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

Prevention

When it happens

Trigger: Calling EndInit() on a FrameworkContentElement whose InitPending flag is false - i.e., no BeginInit was called, or EndInit was already called once for the current BeginInit.

Common situations: Double-EndInit in error-prone factory code (EndInit in both a catch block and normal path); calling EndInit on elements that the XAML loader initializes internally.

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

Appendix: source

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

            // 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
            WriteInternalFlag(InternalFlags.InitPending, false);

            // Mark the element initialized and fire Initialized event
            // (eg. tree building via parser)
            TryFireInitialized();
        }

        /// <summary>
        ///     Has this element been initialized
        /// </summary>
        /// <remarks>
        ///     True if either EndInit or OnParentChanged were called
        /// </remarks>
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public bool IsInitialized

View on GitHub (pinned to 81131a70a4)