dotnet/wpf · error · InvalidOperationException

SR.EndInitWithoutBeginInitNotSupported

Error message

SR.EndInitWithoutBeginInitNotSupported

What it means

ResourceDictionary.EndInit throws InvalidOperationException (SR.EndInitWithoutBeginInitNotSupported) when called without a prior BeginInit, since IsInitializePending is false. EndInit completes a batch initialization and requires the matching BeginInit.

Solutions

  1. Call BeginInit before EndInit, keeping the pair in the same method or try/finally scope.
  2. Check IsInitializePending (or track your own flag) before invoking EndInit.
  3. Only place EndInit in a finally block if BeginInit is guaranteed to have run first.

Example fix

// before
dictionary.EndInit(); // no BeginInit -> throws
// after
dictionary.BeginInit();
try { /* configure dictionary */ }
finally { dictionary.EndInit(); }
Defensive patterns

Strategy: try-catch

Validate before calling

if (!dictionary.IsInitializePending) throw new InvalidOperationException("EndInit called without a matching BeginInit");

Type guard

bool CanEndInit(ResourceDictionary rd) => rd.IsInitializePending;

Try / catch

try { dictionary.EndInit(); } catch (InvalidOperationException ex) when (ex.Message.Contains("BeginInit")) { log.Warn("EndInit without BeginInit ignored"); }

Prevention

When it happens

Trigger: Calling EndInit on a ResourceDictionary that was never BeginInit-ed, or calling EndInit twice (the second call after IsInitializePending was cleared by the first).

Common situations: XAML loader/deferred-content helpers calling EndInit unconditionally; exception-recovery code that calls EndInit in a finally block even when BeginInit was never reached.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs:1011

            }

            IsInitializePending = true;
            IsInitialized = false;
        }

        /// <summary>
        ///     Fire Invalidation at the end of Init phase
        /// </summary>
        /// <remarks>
        ///     BeginInit and EndInit follow a transaction model. BeginInit marks the
        ///     dictionary uninitialized and EndInit marks it initialized.
        /// </remarks>
        public void EndInit()
        {
            // EndInit without a BeginInit isn't permitted
            if (!IsInitializePending)
            {
                throw new InvalidOperationException(SR.EndInitWithoutBeginInitNotSupported);
            }
            Debug.Assert(!IsInitialized, "Dictionary should not be initialized when EndInit is called");

            IsInitializePending = false;
            IsInitialized = true;

            // Fire Invalidations collectively for all changes made during the Init Phase
            NotifyOwners(new ResourcesChangeInfo(null, this));
        }

        #endregion ISupportInitialize

        #region DeferContent

        private bool CanCache(KeyRecord keyRecord, object value)
        {
            if (keyRecord.SharedSet)
            {

View on GitHub (pinned to 81131a70a4)