dotnet/wpf · error · ArgumentException

SR.StreamDoesNotSupportSeek

Error message

SR.StreamDoesNotSupportSeek

What it means

The XmlStreamStore(Stream) constructor requires a seekable stream because the store must reposition within the annotation XML stream. If stream.CanSeek is false it throws ArgumentException(SR.StreamDoesNotSupportSeek). Non-seekable streams (e.g. network or decompression streams read straight through) cannot back the store.

Solutions

  1. Copy the stream into a seekable MemoryStream first, then construct the store.
  2. Write the data to a FileStream and construct the store from the file stream.
  3. If you control the source, supply a stream type with CanSeek == true (e.g. FileStream, MemoryStream).

Example fix

// before
using var store = new XmlStreamStore(httpResponseStream); // throws: not seekable
// after
using var ms = new MemoryStream();
httpResponseStream.CopyTo(ms);
ms.Position = 0;
using var store = new XmlStreamStore(ms);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!stream.CanSeek)
{
    var ms = new MemoryStream();
    stream.CopyTo(ms);
    ms.Position = 0;
    stream = ms;
}

Type guard

static Stream EnsureSeekable(Stream s) => s.CanSeek ? s : (() => { var ms = new MemoryStream(); s.CopyTo(ms); ms.Position = 0; return (Stream)ms; })();

Try / catch

try { using var store = new XmlStreamStore(stream); }
catch (ArgumentException ex) when (ex.Message.Contains("seek")) { /* buffer into MemoryStream and retry */ }

Prevention

When it happens

Trigger: new XmlStreamStore(new NetworkStream(...)), new XmlStreamStore(responseStream) from an HttpClient response body, or a CryptoStream/DeflateStream chain where CanSeek is false.

Common situations: Downloading annotation files over HTTP and passing the response stream directly; wrapping the store around a compression stream; piping streams between processes.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

            _predefinedNamespaces.Add(new Uri(AnnotationXmlConstants.Namespaces.BaseSchemaNamespace), null);
            _predefinedNamespaces.Add(new Uri(XamlReaderHelper.DefaultNamespaceURI), null);
        }

        /// <summary>
        ///     Creates an instance using the XML stream passed in as the
        ///     content. The XML in the stream must be valid XML and conform
        ///     to the CAF 2.0 schema.
        /// </summary>
        /// <param name="stream">stream containing annotation data in XML format</param>
        /// <exception cref="ArgumentNullException">stream is null</exception>
        /// <exception cref="XmlException">stream contains invalid XML</exception>
        public XmlStreamStore(Stream stream)
            : base()
        {
            ArgumentNullException.ThrowIfNull(stream);

            if (!stream.CanSeek)
                throw new ArgumentException(SR.StreamDoesNotSupportSeek);

            SetStream(stream, null);
        }

        /// <summary>
        ///     Creates an instance using the XML stream passed in as the
        ///     content. The XML in the stream must be valid XML and conform
        ///     to the Annotations V1 schema or a valid future version XML which
        ///     compatibility rules are that when applied they will produce
        ///     a valid Annotations V1 XML. This .ctor allows registration of
        ///     application specific known namespaces.
        /// </summary>
        /// <param name="stream">stream containing annotation data in XML format</param>
        /// <param name="knownNamespaces">A dictionary with known and compatible namespaces. The keys in
        /// this dictionary are known namespaces. The value of each key is a list of namespaces that are compatible with
        /// the key one, i.e. each of the namespaces in the value list will be transformed to the
        /// key namespace while reading the input XML.</param>
        /// <exception cref="ArgumentNullException">stream is null</exception>

View on GitHub (pinned to 81131a70a4)