dotnet/wpf · error · InvalidOperationException

SR.OnlyOneInitialization

Error message

SR.OnlyOneInitialization

What it means

GlyphTypeface implements ISupportInitialize. BeginInit throws SR.OnlyOneInitialization when the GlyphTypeface is already fully initialized (_initializationState == InitializationState.IsInitialized), because its two-phase initialize-once lifecycle forbids re-initialization. This is an InvalidOperationException raised from the public ISupportInitialize.BeginInit method in GlyphTypeface.cs.

Solutions

  1. Do not call BeginInit again on the same GlyphTypeface; create a new GlyphTypeface for the new font/URI.
  2. Track your own initialization flag and skip BeginInit when the instance is already initialized.
  3. If the font face must change, dispose of/recreate the GlyphTypeface rather than re-initializing it.

Example fix

// before
var face = new GlyphTypeface(fontUri);
((ISupportInitialize)face).BeginInit(); // throws if already initialized

// after
if (((ISupportInitialize)face).IsInitialized) // or track your own flag
{
    face = new GlyphTypeface(fontUri); // create a fresh instance instead of re-initializing
}
((ISupportInitialize)face).BeginInit();
Defensive patterns

Strategy: type-guard

Validate before calling

bool isInitialized = ((ISupportInitialize)face).IsInitialized;
if (isInitialized) { face = new GlyphTypeface(fontUri); }

Type guard

static bool CanBeginInit(GlyphTypeface face) => !((ISupportInitialize)face).IsInitialized;

Try / catch

try { ((ISupportInitialize)face).BeginInit(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("initializ")) { /* instance already initialized; reuse as-is or recreate */ }

Prevention

When it happens

Trigger: Calling ISupportInitialize.BeginInit() (or ((ISupportInitialize)glyphTypeface).BeginInit()) on a GlyphTypeface instance that has already completed a full BeginInit/EndInit cycle, i.e. calling BeginInit twice without constructing a new GlyphTypeface in between.

Common situations: Reusing a cached GlyphTypeface for a second font load; component designer/serializer code that calls BeginInit on an existing instance; shared helper methods that defensively call BeginInit on objects that may already be initialized; re-running XAML load logic that re-initializes font resources.

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

Appendix: source

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

            {
                return localizedStrings;
            }
            else
            {
                return new MS.Internal.Text.TextInterface.LocalizedStrings();
            }
        }
 
        #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);
            }

View on GitHub (pinned to 81131a70a4)