dotnet/wpf · error · InvalidOperationException

SR.InInitialization

Error message

SR.InInitialization

What it means

GlyphTypeface's ISupportInitialize.BeginInit throws SR.InInitialization when called while the GlyphTypeface is already mid-initialization (_initializationState == InitializationState.IsInitializing). A BeginInit/EndInit cycle cannot be nested or restarted before EndInit completes, so a second BeginInit during an open cycle is rejected with InvalidOperationException.

Solutions

  1. Ensure BeginInit is called exactly once per cycle, followed by EndInit before any further property setup.
  2. Wrap the EndInit call in try/finally so a failed EndInit does not leave the instance in the IsInitializing state.
  3. Discard the half-initialized instance and construct a new GlyphTypeface instead of re-calling BeginInit.

Example fix

// before
face.BeginInit();
face.BeginInit(); // throws InInitialization
face.EndInit();

// after
face.BeginInit();
face.UriSource = fontUri;
face.EndInit(); // single Begin/EndInit pair per cycle
Defensive patterns

Strategy: validation

Validate before calling

// Guard wrapper: only BeginInit when not already initializing/initialized
static void SafeBeginInit(GlyphTypeface face) { if (((ISupportInitialize)face).IsInitialized) throw new InvalidOperationException("Already fully initialized"); /* track an isInitializing flag in your wrapper to prevent nesting */ }

Type guard

static bool IsMidInitialization(bool beginCalled, bool endCalled) => beginCalled && !endCalled;

Try / catch

try { ((ISupportInitialize)face).BeginInit(); }
catch (InvalidOperationException) { /* a BeginInit cycle is already open; skip or recreate */ }

Prevention

When it happens

Trigger: Calling ISupportInitialize.BeginInit() twice in a row without a successful EndInit() in between; nested BeginInit calls from wrapper code that calls BeginInit both in a setup helper and again inline.

Common situations: Code generated by designers that emits an extra BeginInit; exception in EndInit leaves the instance in IsInitializing state and recovery code calls BeginInit again; wrapper libraries that call BeginInit defensively before delegating to user code that also calls it.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs:1656

            }
        }
 
        #endregion Private Methods

        #region ISupportInitialize interface

        void ISupportInitialize.BeginInit()
        {
            if (_initializationState == InitializationState.IsInitialized)
            {
                // Cannot initialize a GlyphRun this is completely initialized.
                throw new InvalidOperationException(SR.OnlyOneInitialization);
            }

            if (_initializationState == InitializationState.IsInitializing)
            {
                // Cannot initialize a GlyphRun this already being initialized.
                throw new InvalidOperationException(SR.InInitialization);
            }

            _initializationState = InitializationState.IsInitializing;
        }

        void ISupportInitialize.EndInit()
        {
            if (_initializationState != InitializationState.IsInitializing)
            {
                // Cannot EndInit a GlyphRun that is not being initialized.
                throw new InvalidOperationException(SR.NotInInitialization);
            }

            Initialize(_originalUri, _styleSimulations);
        }

        private void CheckInitialized()
        {

View on GitHub (pinned to 81131a70a4)