dotnet/wpf · error · InvalidOperationException

SR.Format(SR.ResourceDictionaryLoadFromFailure…

Error message

SR.Format(SR.ResourceDictionaryLoadFromFailure, _source.ToString())

What it means

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.

Solutions

  1. Verify the Source Uri points at a compiled XAML ResourceDictionary (Build Action = Page/Resource) that exists in the assembly.
  2. Test the pack URI resolves correctly (pack://application:,,,/AssemblyName;component/path/file.xaml).
  3. Check the web/deployment target actually serves the .xaml content, not an error page, for externally hosted dictionaries.

Example fix

// before
dictionary.Source = new Uri("pack://application:,,,/Themes/Thme.xaml");
// after
dictionary.Source = new Uri("pack://application:,,,/Themes/Theme.xaml");
Defensive patterns

Strategy: try-catch

Validate before calling

Uri uri;
if (!Uri.TryCreate(path, UriKind.Absolute, out uri)) throw new ArgumentException($"Invalid dictionary URI: {path}");
// additionally verify the pack resource exists in the assembly before assigning Source

Try / catch

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); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs:227

                        }
                        return;
                    }
                    else
                    {
                        throw;
                    }
                }

                // MimeObjectFactory.GetObjectAndCloseStream will try to find the object converter basing on the mime type.
                // It can be a sync/async converter. It's the converter's responsiblity to close the stream.
                // If it fails to find a convert, this call will return null.
                System.Windows.Markup.XamlReader asyncObjectConverter;
                ResourceDictionary loadedRD = MimeObjectFactory.GetObjectAndCloseStreamCore(s, contentType, uri, false, false, false /*allowAsync*/, false /*isJournalNavigation*/, out asyncObjectConverter, IsUnsafe)
                                            as ResourceDictionary;

                if (loadedRD == null)
                {
                    throw new InvalidOperationException(SR.Format(SR.ResourceDictionaryLoadFromFailure, _source.ToString()));
                }

                // ReferenceCopy all the key-value pairs in the _baseDictionary
                _baseDictionary = loadedRD._baseDictionary;

                // ReferenceCopy all the entries in the MergedDictionaries collection
                _mergedDictionaries = loadedRD._mergedDictionaries;

                // ReferenceCopy all of the deferred content state
                CopyDeferredContentFrom(loadedRD);

                // Take over the deferred resource references
                MoveDeferredResourceReferencesFrom(loadedRD);

                // Copy over the HasImplicitStyles flag
                HasImplicitStyles = loadedRD.HasImplicitStyles;

                // Copy over the HasImplicitDataTemplates flag

View on GitHub (pinned to 81131a70a4)