dotnet/wpf · error · InvalidOperationException

SR.StreamNotSet

Error message

SR.StreamNotSet

What it means

XmlStreamStore.CheckStatus() throws InvalidOperationException (SR.StreamNotSet) when the store was constructed without a backing Stream (_stream == null). The store needs a stream to read/write annotation XML; without one no operation can proceed. This is a configuration error at store construction.

Solutions

  1. Construct the XmlStreamStore with a valid Stream (e.g. new XmlStreamStore(stream))
  2. If a custom subclass, assign _stream (or call the base constructor with the stream) before use
  3. Check _stream != null (or wrap setup) before performing operations

Example fix

// before
var store = new XmlStreamStore();
store.Flush(); // InvalidOperationException: StreamNotSet

// after
using var stream = File.Open("annotations.xml", FileMode.OpenOrCreate);
var store = new XmlStreamStore(stream);
store.Flush();
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a stream-backed store before use
var store = stream != null ? new XmlStreamStore(stream) : throw new InvalidOperationException("Annotation stream not initialized");

Try / catch

try { store.AddAnnotation(a); }
catch (InvalidOperationException ex) when (ex.Message.Contains("stream", StringComparison.OrdinalIgnoreCase)) { InitializeStreamAndRetry(); }

Prevention

When it happens

Trigger: Creating XmlStreamStore with the parameterless/default path that does not assign a stream, then calling AddAnnotation, DeleteAnnotation, GetAnnotation, Flush, FindAnnotationIds, or InternalGetAnnotations.

Common situations: Using a constructor overload that defers stream assignment and forgetting to set it; passing a stream that was disposed and nulled elsewhere; copy-pasting store setup code and dropping the stream argument.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Storage/XmlStreamStore.cs:933

                    navigator = (XPathNavigator)iterator.Current;
                }
            }

            return navigator;
        }

        /// <summary>
        ///    Verifies the store is in a valid state.  Throws exceptions otherwise.
        /// </summary>
        private void CheckStatus()
        {
            lock (SyncRoot)
            {
                if (IsDisposed)
                    throw new ObjectDisposedException(null, SR.ObjectDisposed_StoreClosed);

                if (_stream == null)
                    throw new InvalidOperationException(SR.StreamNotSet);
            }
        }

        /// <summary>
        /// Called from flush to serialize all annotations in the map
        /// notice that delete takes care of the delete action both in the map
        /// and in the store
        /// </summary>
        private void SerializeAnnotations()
        {
            List<Annotation> mapAnnotations = _storeAnnotationsMap.FindDirtyAnnotations();
            foreach (Annotation annotation in mapAnnotations)
            {
                XPathNavigator editor = GetAnnotationNodeForId(annotation.Id);
                if (editor == null)
                {
                    editor = (XPathNavigator)_rootNavigator.CreateNavigator();
                    XmlWriter writer = editor.AppendChild();

View on GitHub (pinned to 81131a70a4)