dotnet/wpf · error · InvalidOperationException

InitializationState !=

Error message

InitializationState != 

What it means

System.Xaml's internal ConcurrentDictionary<K,V> throws KeyNotFoundException when the indexer getter is called with a key that is not present in the underlying hashtable. Unlike Dictionary<K,V>, this lightweight internal implementation has no TryGetValue, so a missing key is always a hard exception.

Solutions

  1. Verify the key actually exists before indexing; add it first if this is your own code path (use ContainsKey or the TryGetValue/Add members if available).
  2. For XamlSchemaContext/xmlns lookup failures, ensure the assembly carries correct XmlnsDefinition attributes and that InternalsVisibleTo entries are intact.
  3. If a race is suspected, synchronize the write of the key before any reader dereferences it, or retry the lookup after initialization completes.
  4. If this originates inside System.Xaml itself, capture the missing key value and file a WPF issue; there is no TryGetValue fallback in this internal type.

Example fix

// before
var clrNs = nsMap[xmlNs]; // KeyNotFoundException if absent
// after
if (nsMap.ContainsKey(xmlNs))
{
    var clrNs = nsMap[xmlNs];
}
else
{
    // register or skip
}
Defensive patterns

Strategy: validation

Validate before calling

// see 4850

Prevention

When it happens

Trigger: Reading dict[key] where key was never added (or was added by another thread that has not yet published the value); the getter does obj result = _hashtable[key] and throws when result == null.

Common situations: XAML type/schema lookups where a namespace or assembly mapping was never registered; races where code assumes a key exists because a related API returned successfully; reflection over assemblies lacking the expected xmlns mapping.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/c3c7c4ef32b68089. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/Common/src/System/IO/Compression/DeflateZLib/ZLibNative.cs:243

                set { _zStream.nextOut = value; }
            }

            public uint AvailOut
            {
                get { return _zStream.availOut; }
                set { _zStream.availOut = value; }
            }

            private void EnsureNotDisposed()
            {
                ObjectDisposedException.ThrowIf(InitializationState == State.Disposed, this);
            }


            private void EnsureState(State requiredState)
            {
                if (InitializationState != requiredState)
                    throw new InvalidOperationException("InitializationState != " + requiredState.ToString());
            }


            public ErrorCode DeflateInit2_(CompressionLevel level, int windowBits, int memLevel, CompressionStrategy strategy)
            {
                EnsureNotDisposed();
                EnsureState(State.NotInitialized);

                ErrorCode errC = Interop.Zlib.DeflateInit2_(ref _zStream, level, CompressionMethod.Deflated, windowBits, memLevel, strategy);
                _initializationState = State.InitializedForDeflate;

                return errC;
            }


            public ErrorCode Deflate(FlushCode flush)
            {
                EnsureNotDisposed();

View on GitHub (pinned to 81131a70a4)