unoplatform/uno · error · InvalidOperationException
serviceProvider does not implement IXamlNameResolver
Error message
serviceProvider does not implement IXamlNameResolver
What it means
Thrown by the Reference markup extension (x:Reference) when ProvideValue cannot obtain an IXamlNameResolver service from the supplied IServiceProvider. The XAML object writer is expected to supply this service so that forward/name references can be resolved at load time. Its absence means the host is not a conforming XAML reader/writer stack.
Source
Thrown at src/SourceGenerators/System.Xaml/System.Windows.Markup/Reference.cs:63
[ConstructorArgument ("name")]
public string Name { get; set; }
public override object ProvideValue (IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException ("serviceProvider");
}
if (Name == null)
{
throw new InvalidOperationException ("Name property is not set");
}
var r = serviceProvider.GetService (typeof (IXamlNameResolver)) as IXamlNameResolver;
if (r == null)
{
throw new InvalidOperationException ("serviceProvider does not implement IXamlNameResolver");
}
var ret = r.Resolve (Name);
if (ret == null)
{
ret = r.GetFixupToken (new string [] {Name}, true);
}
return ret;
}
}
}
View on GitHub (pinned to 0418340488)
Solutions
- Do not call ProvideValue manually; let the XamlObjectWriter / XamlXmlReader pipeline invoke it, which supplies IXamlNameResolver.
- If testing, supply a fake IServiceProvider whose GetService returns a IXamlNameResolver implementation for typeof(IXamlNameResolver).
- Ensure the XAML schema context has been initialized with a name-scope so the resolver is non-null during load.
- Verify x:Reference targets a name that exists within the same namescope.
Example fix
// before
var ext = new Reference { Name = "myBtn" };
var value = ext.ProvideValue(new ServiceLocator()); // throws
// after
var value = ext.ProvideValue(XamlObjectWriterContext.WithNameResolver(resolver)); Defensive patterns
Strategy: validation
Validate before calling
// Validate the service provider before invoking the extension
public static bool CanResolveReference(IServiceProvider sp)
{
return sp?.GetService(typeof(IXamlNameResolver)) is IXamlNameResolver;
}
// Usage
if (!CanResolveReference(sp)) throw new InvalidOperationException("Name resolver missing");
var value = new Reference { Name = "btn" }.ProvideValue(sp); Type guard
static bool HasNameResolver(IServiceProvider sp)
=> sp?.GetService(typeof(IXamlNameResolver)) is IXamlNameResolver; Prevention
- Always invoke markup extensions through the XamlObjectWriter pipeline rather than calling ProvideValue directly.
- When unit-testing extensions, supply a fake IServiceProvider that advertises IXamlNameResolver.
- Treat a null IXamlNameResolver as a host-configuration defect, not a runtime data error.
When it happens
Trigger: Calling Reference.ProvideValue(serviceProvider) where serviceProvider.GetService(typeof(IXamlNameResolver)) returns null. Happens when invoking the markup extension outside a real XAML load context, or with a stub/null service provider that does not advertise name-resolution services.
Common situations: Unit tests that call ProvideValue directly with a mock IServiceProvider; custom XAML writers that forget to register IXamlNameResolver; invoking markup extensions from non-XAML code paths (e.g. manual template instantiation).
Related errors
- serviceProvider does not provide IXamlTypeResolver service.
- Member property must be set to StaticExtension before callin
- Member '{0}' could not be resolved to a static member
- typeName
- type
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/41c1e6886555fc31.
Report an issue: GitHub.