dotnet/wpf · error · InvalidOperationException
SR.Format(SR.MarkupExtensionNoContext, GetType().Name…
Error message
SR.Format(SR.MarkupExtensionNoContext, GetType().Name, "IProvideValueTarget")
What it means
ProvideValue needs the WPF XAML parser's IProvideValueTarget service to learn which object/property the extension is being applied to. When the service provider returns null (extension used outside a XAML parse, e.g. in design-time tools or manually constructed markup-extension evaluation), it throws InvalidOperationException naming MarkupExtensionNoContext.
Solutions
- Only evaluate the extension through the WPF XAML loader, which supplies IProvideValueTarget.
- In custom hosts, provide a service provider implementing IProvideValueTarget that returns the target ResourceDictionary and its Source property.
- Construct the theme-sourced dictionary directly in code (set ResourceDictionary.Source to a theme-pack URI) instead of using the extension.
Example fix
// before
var sp = new ServiceProvider(); // no IProvideValueTarget
var uri = (Uri)ext.ProvideValue(sp); // throws
// after
var rd = new ResourceDictionary
{
Source = new Uri("pack://application:,,,/MyApp;component/Themes/Default.xaml")
}; Defensive patterns
Strategy: try-catch
Validate before calling
var target = serviceProvider?.GetService(typeof(IProvideValueTarget));
if (target == null) throw new InvalidOperationException("ProvideValue requires IProvideValueTarget from a XAML host."); Type guard
bool HasContext(IServiceProvider sp) => sp?.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget;
Try / catch
try { return (Uri)ext.ProvideValue(sp); }
catch (InvalidOperationException ex) when (ex.Message.Contains("IProvideValueTarget")) { return FallbackUri(); } Prevention
- Evaluate markup extensions only via the XAML loader.
- In custom hosts, implement and register IProvideValueTarget.
- Prefer setting ResourceDictionary.Source directly in code.
When it happens
Trigger: Calling ThemeDictionaryExtension.ProvideValue with an IServiceProvider that does not implement IProvideValueTarget; evaluating the extension outside of XAML loading (unit tests, code-generated dictionaries).
Common situations: Unit tests that build ResourceDictionaries in code and evaluate markup extensions manually; custom XAML loaders missing the standard service provider; designer/host environments that partially implement the service provider.
Understand the failure class
Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.
Related errors
- SR.Format(SR.MarkupExtensionNoContext, GetType().Name…
- SR.MarkupExtensionNoContext
- IAmbientProvider
- IXamlSchemaContextProvider
- NotImplementedException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9a1e4241ee430ae5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeDictionaryExtension.cs:74
/// Return an object that should be set on the targetObject's targetProperty
/// for this markup extension. For ThemeDictionaryExtension, this is the Uri
/// pointing to theme specific dictionary in the specified assembly by AssemblyName.
/// </summary>
/// <param name="serviceProvider">ServiceProvider that can be queried for services.</param>
/// <returns>
/// The object to set on this property.
/// </returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (string.IsNullOrEmpty(AssemblyName))
{
throw new InvalidOperationException(SR.ThemeDictionaryExtension_Name);
}
IProvideValueTarget provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if( provideValueTarget == null )
{
throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, "IProvideValueTarget" ));
}
object targetObject = provideValueTarget.TargetObject;
object targetProperty = provideValueTarget.TargetProperty;
ResourceDictionary dictionary = targetObject as ResourceDictionary;
PropertyInfo propertyInfo = targetProperty as PropertyInfo;
// Allow targetProperty to be null or ResourceDictionary.Source
if (dictionary == null || (targetProperty != null && propertyInfo != SourceProperty))
{
throw new InvalidOperationException(SR.ThemeDictionaryExtension_Source);
}
Register(dictionary, _assemblyName);
dictionary.IsSourcedFromThemeDictionary = true;
return GenerateUri(_assemblyName, SystemResources.ResourceDictionaries.ThemedResourceName, MS.Win32.UxThemeWrapper.ThemeName);View on GitHub (pinned to 81131a70a4)