{"record":{"id":"fd20f30811ff6cf1","repo":"dotnet/wpf","slug":"sr-expectedbinarycontent","errorCode":null,"errorMessage":"SR.ExpectedBinaryContent","messagePattern":"SR\\.ExpectedBinaryContent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DeferrableContentConverter.cs","lineNumber":63,"sourceCode":"\n                ResourceDictionary dictionary = ipvt.TargetObject as ResourceDictionary;\n                if (dictionary == null)\n                {\n                    throw new InvalidOperationException(SR.ExpectedResourceDictionaryTarget);\n                }\n\n                Stream stream = value as Stream;\n                if (stream == null)\n                {\n                    byte[] bytes = value as byte[];\n                    if (bytes != null)\n                    {\n                        stream = new MemoryStream(bytes);\n                    }\n                }\n                if (stream == null)\n                {\n                    throw new InvalidOperationException(SR.ExpectedBinaryContent);\n                }\n\n                // we shouldn't pass around the service provider\n                DeferrableContent deferrableContext = new DeferrableContent(stream, schemaContext,\n                    objectWriterFactory, context, rootObjectProvider.RootObject);\n                return deferrableContext;\n            }\n\n            return base.ConvertFrom(context, culture, value);\n        }\n\n        private static T RequireService<T>(IServiceProvider provider) where T : class\n        {\n            T result = provider.GetService(typeof(T)) as T;\n            if (result == null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.DeferringLoaderNoContext, nameof(DeferrableContentConverter), typeof(T).Name));\n            }","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DeferrableContentConverter.cs#L45-L81","documentation":"ConvertFrom expects the incoming value to be deferred content binary data: either a Stream or a byte[] (which it wraps in a MemoryStream). If the value is neither, it throws InvalidOperationException(SR.ExpectedBinaryContent) because no binary content can be deferred.","triggerScenarios":"Passing ConvertFrom a value that is not a Stream or byte[] — for example a string of XAML, an XmlReader, or null — while the schema context and target dictionary checks already passed.","commonSituations":"Hand-rolling deferred-content conversion from raw XAML text instead of the compiled BAML stream, deserializing an old/persisted resource format where content was stored as text, or a loader pipeline that decoded the BAML into a different representation.","solutions":["Pass the raw BAML Stream (from Application resource stream, e.g. Application.GetResourceStream) to the converter","If you have byte[], pass the array directly — it is wrapped in a MemoryStream internally","Do not pass XAML text/strings; recompile the XAML to BAML or use XamlReader for text-based XAML"],"exampleFix":"// before\nconverter.ConvertFrom(null, CultureInfo.InvariantCulture, xamlText);\n// after\nusing (var stream = Application.GetResourceStream(new Uri(\"mydict.xaml\", UriKind.Relative)).Stream)\n    converter.ConvertFrom(null, CultureInfo.InvariantCulture, stream);","handlingStrategy":"type-guard","validationCode":"if (value is not Stream and not byte[])\n    throw new ArgumentException(\"Deferrable content must be a Stream or byte[].\");","typeGuard":"static bool IsDeferredBinaryContent(object v) => v is Stream or byte[];","tryCatchPattern":"try { return (DeferrableContent)converter.ConvertFrom(ctx, culture, value); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"binary\")) { /* convert/compile to BAML first */ }","preventionTips":["Feed the converter the raw BAML stream from Application.GetResourceStream or the compiled resource","Never pass XAML text or readers to DeferrableContentConverter","Validate the value type (Stream/byte[]) before calling ConvertFrom"],"tags":["wpf","baml","argument"],"backgroundTag":"incompatible-source-type","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"}