{"record":{"id":"5d4903d177d4d1eb","repo":"dotnet/wpf","slug":"sr-parsercannotreusexamlreader","errorCode":null,"errorMessage":"SR.ParserCannotReuseXamlReader","messagePattern":"SR\\.ParserCannotReuseXamlReader","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlReader.cs","lineNumber":206,"sourceCode":"        /// The load operation will be done asynchronously if the\n        /// markup specifies x:SynchronousMode=\"async\".\n        /// </summary>\n        /// <param name=\"stream\">stream for the xml content</param>\n        /// <param name=\"useRestrictiveXamlReader\">Whether or not this method should use \n        /// RestrictiveXamlXmlReader to restrict instantiation of potentially dangerous types</param>\n        /// <returns>object root generated after xml parsed</returns>\n        /// <remarks>\n        /// Notice that this is an instance method\n        /// </remarks>\n        public object LoadAsync(Stream stream, bool useRestrictiveXamlReader)\n        {\n            ArgumentNullException.ThrowIfNull(stream);\n            _stream = stream;\n\n            if (_objectWriter != null)\n            {\n                // A XamlReader instance cannot be shared across two load operations\n                throw new InvalidOperationException(SR.ParserCannotReuseXamlReader);\n            }\n\n            return LoadAsync(stream, null, useRestrictiveXamlReader);\n        }\n\n        /// <summary>\n        /// Reads XAML using the given XmlReader, building an object tree.\n        /// The load operation will be done asynchronously if the markup\n        /// specifies x:SynchronousMode=\"async\".\n        /// </summary>\n        /// <param name=\"reader\">Reader for xml content.</param>\n        /// <returns>object root generated after xml parsed</returns>\n        /// <remarks>\n        /// Notice that this is an instance method\n        /// </remarks>\n        public object LoadAsync(XmlReader reader)\n        {\n","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlReader.cs#L188-L224","documentation":"XamlReader.LoadAsync stores the stream and lazily creates an object writer; a single XamlReader instance cannot run two load operations concurrently or sequentially. If _objectWriter is already set (a previous LoadAsync in progress or completed), the second LoadAsync(Stream, bool) call throws InvalidOperationException(SR.ParserCannotReuseXamlReader).","triggerScenarios":"Calling LoadAsync on the same XamlReader instance a second time — e.g. reusing a cached reader for another stream, or calling LoadAsync again after an earlier load (including one still running).","commonSituations":"Caching a XamlReader to 'reuse' it across multiple XAML loads, issuing overlapping async loads from UI events, retry logic that calls LoadAsync on the same instance after failure.","solutions":["Create a new XamlReader instance for each load operation.","Use the static XamlReader.LoadAsync(stream) overload, which handles instance lifecycle internally.","If retries are needed, instantiate a fresh reader inside the retry loop."],"exampleFix":"// before\n_reader = new XamlReader();\nvar r1 = await _reader.LoadAsync(stream1);\nvar r2 = await _reader.LoadAsync(stream2); // throws\n// after\nvar r1 = await new XamlReader().LoadAsync(stream1);\nvar r2 = await new XamlReader().LoadAsync(stream2);","handlingStrategy":"validation","validationCode":"// ensure a fresh reader per load; never reuse\nif (readerHasBeenUsed) reader = new XamlReader();\nvar result = await reader.LoadAsync(stream, useRestrictiveXamlReader);","typeGuard":null,"tryCatchPattern":"try { return await reader.LoadAsync(stream); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"cannot be shared\")) { reader = new XamlReader(); return await reader.LoadAsync(stream); }","preventionTips":["Treat XamlReader as single-use; create one per load operation.","Never cache XamlReader instances in fields or DI containers as singletons.","Prefer the static XamlReader.LoadAsync overloads for ad-hoc loads."],"tags":["wpf","xaml","async","object-lifetime"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}