{"record":{"id":"b0bf9dae527a0504","repo":"dotnet/wpf","slug":"sr-expectedresourcedictionarytarget","errorCode":null,"errorMessage":"SR.ExpectedResourceDictionaryTarget","messagePattern":"SR\\.ExpectedResourceDictionaryTarget","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DeferrableContentConverter.cs","lineNumber":49,"sourceCode":"                XamlSchemaContext xamlSchemaContext =\n                    RequireService<IXamlSchemaContextProvider>(context).SchemaContext;\n                Baml2006SchemaContext schemaContext = xamlSchemaContext as Baml2006SchemaContext;\n                if (schemaContext == null)\n                {\n                    throw new InvalidOperationException(SR.ExpectedBamlSchemaContext);\n                }\n\n                IXamlObjectWriterFactory objectWriterFactory =\n                    RequireService<IXamlObjectWriterFactory>(context);\n                IProvideValueTarget ipvt =\n                    RequireService<IProvideValueTarget>(context);\n                IRootObjectProvider rootObjectProvider =\n                    RequireService<IRootObjectProvider>(context);\n\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,","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DeferrableContentConverter.cs#L31-L67","documentation":"Inside ConvertFrom, after obtaining IProvideValueTarget, the converter checks that the target object receiving the deferred content is a ResourceDictionary. Deferred content is only meaningful inside a ResourceDictionary; if the value target is any other object the converter throws InvalidOperationException.","triggerScenarios":"Invoking DeferrableContentConverter.ConvertFrom when the IProvideValueTarget.TargetObject returned by the service provider is not a ResourceDictionary — i.e. the deferral loader is being applied outside a dictionary, such as directly in a control's property or in a non-dictionary resource scope.","commonSituations":"Manually constructing a service provider for a ValueSerializer/TypeConverter test, copying a DeferringLoader attribute onto a type not used inside ResourceDictionary, or WPF internals changed by a framework version mismatch so the target is typed differently.","solutions":["Only use DeferrableContentConverter for values that live inside a ResourceDictionary (e.g. deferred BAML entries of a .xaml resource dictionary)","Remove a custom DeferringLoader/ValueSerializer assignment from types that are not ResourceDictionary content","If building a test harness, register an IProvideValueTarget whose TargetObject is a real ResourceDictionary instance"],"exampleFix":"// before\ntypeDescriptorContext.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget\n// returns a target that is not a ResourceDictionary\n// after\nvar ipvt = (IProvideValueTarget)provider.GetService(typeof(IProvideValueTarget));\nif (ipvt.TargetObject is ResourceDictionary) { /* safe to proceed */ }","handlingStrategy":"type-guard","validationCode":"var ipvt = provider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;\nif (ipvt?.TargetObject is not ResourceDictionary)\n    throw new InvalidOperationException(\"DeferrableContentConverter may only target a ResourceDictionary.\");","typeGuard":"static bool TargetsResourceDictionary(IServiceProvider p) =>\n    (p.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget)?.TargetObject is ResourceDictionary;","tryCatchPattern":"try { return (DeferrableContent)converter.ConvertFrom(ctx, culture, value); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ResourceDictionary\")) { /* handle non-dictionary target */ }","preventionTips":["Only attach the WPF deferring loader to types consumed inside ResourceDictionary","In custom pipelines, verify IProvideValueTarget.TargetObject type before invoking the converter","Don't reuse DeferrableContentConverter for generic deferred content scenarios"],"tags":["wpf","xaml","resourcedictionary"],"backgroundTag":"type-mismatch","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"}