{"record":{"id":"fef1cc194d3984b8","repo":"dotnet/wpf","slug":"sr-format-sr-resourcedictionaryloadfromfailure-source","errorCode":null,"errorMessage":"SR.Format(SR.ResourceDictionaryLoadFromFailure, _source.ToString())","messagePattern":"SR\\.Format\\(SR\\.ResourceDictionaryLoadFromFailure, _source\\.ToString\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs","lineNumber":227,"sourceCode":"                        }\n                        return;\n                    }\n                    else\n                    {\n                        throw;\n                    }\n                }\n\n                // MimeObjectFactory.GetObjectAndCloseStream will try to find the object converter basing on the mime type.\n                // It can be a sync/async converter. It's the converter's responsiblity to close the stream.\n                // If it fails to find a convert, this call will return null.\n                System.Windows.Markup.XamlReader asyncObjectConverter;\n                ResourceDictionary loadedRD = MimeObjectFactory.GetObjectAndCloseStreamCore(s, contentType, uri, false, false, false /*allowAsync*/, false /*isJournalNavigation*/, out asyncObjectConverter, IsUnsafe)\n                                            as ResourceDictionary;\n\n                if (loadedRD == null)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.ResourceDictionaryLoadFromFailure, _source.ToString()));\n                }\n\n                // ReferenceCopy all the key-value pairs in the _baseDictionary\n                _baseDictionary = loadedRD._baseDictionary;\n\n                // ReferenceCopy all the entries in the MergedDictionaries collection\n                _mergedDictionaries = loadedRD._mergedDictionaries;\n\n                // ReferenceCopy all of the deferred content state\n                CopyDeferredContentFrom(loadedRD);\n\n                // Take over the deferred resource references\n                MoveDeferredResourceReferencesFrom(loadedRD);\n\n                // Copy over the HasImplicitStyles flag\n                HasImplicitStyles = loadedRD.HasImplicitStyles;\n\n                // Copy over the HasImplicitDataTemplates flag","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs#L209-L245","documentation":"When ResourceDictionary defers loading via Source, it fetches the Uri and asks MimeObjectFactory to produce the content; if the loaded object is not a ResourceDictionary (or null), an InvalidOperationException is thrown with SR.ResourceDictionaryLoadFromFailure naming the source Uri.","triggerScenarios":"Setting Source to a Uri that resolves to content whose MIME/type is not a XAML ResourceDictionary (e.g. an image, HTML, 404 page, or non-XAML file), so GetObjectAndCloseStreamCore returns null or a non-dictionary object.","commonSituations":"Typo in a pack URI pointing at the wrong file; a web-server/deployment returning an HTML error page for the requested theme XAML; wrong Build Action so the XAML is not embedded in the assembly.","solutions":["Verify the Source Uri points at a compiled XAML ResourceDictionary (Build Action = Page/Resource) that exists in the assembly.","Test the pack URI resolves correctly (pack://application:,,,/AssemblyName;component/path/file.xaml).","Check the web/deployment target actually serves the .xaml content, not an error page, for externally hosted dictionaries."],"exampleFix":"// before\ndictionary.Source = new Uri(\"pack://application:,,,/Themes/Thme.xaml\");\n// after\ndictionary.Source = new Uri(\"pack://application:,,,/Themes/Theme.xaml\");","handlingStrategy":"try-catch","validationCode":"Uri uri;\nif (!Uri.TryCreate(path, UriKind.Absolute, out uri)) throw new ArgumentException($\"Invalid dictionary URI: {path}\");\n// additionally verify the pack resource exists in the assembly before assigning Source","typeGuard":null,"tryCatchPattern":"try { rd.Source = uri; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Load from\")) { log.Error($\"Failed to load ResourceDictionary from {uri}: content was not a XAML dictionary\", ex); }","preventionTips":["Verify pack URIs point at XAML files with Build Action = Page/Resource","Test theme URIs in a unit test that loads all merged dictionaries at startup","Check deployed/server content actually serves the .xaml rather than an HTML error page"],"tags":["wpf","resourcedictionary","load-failure"],"backgroundTag":"resource-not-found","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"}