unoplatform/uno · error · ArgumentNullException
serviceProvider
Error message
serviceProvider
What it means
ArgumentNullException thrown by the Reference markup extension's ProvideValue (System.Xaml, ported from Mono) when the IServiceProvider argument is null. Reference (x:Reference) resolves a named element via IXamlNameResolver obtained from the provider, so a null provider makes resolution impossible. Parameter name 'serviceProvider' identifies the null argument.
Source
Thrown at src/SourceGenerators/System.Xaml/System.Windows.Markup/Reference.cs:52
public class Reference : MarkupExtension
{
public Reference ()
{
}
public Reference (string name)
{
Name = name;
}
[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);
}View on GitHub (pinned to 0418340488)
Solutions
- Always pass the IServiceProvider from the XAML runtime; never invoke ProvideValue without one.
- In tests, supply a provider whose GetService returns an IXamlNameResolver.
- Null-check the provider at the call site before invoking.
Example fix
// before var target = reference.ProvideValue(null); // after var target = reference.ProvideValue(xamlServiceProvider);
Defensive patterns
Strategy: validation
Validate before calling
static object SafeProvide(Reference r, IServiceProvider sp) { if (sp is null) throw new ArgumentNullException(nameof(sp)); return r.ProvideValue(sp); } Type guard
static bool HasXamlServices(IServiceProvider sp) => sp?.GetService(typeof(IXamlNameResolver)) is not null;
Try / catch
try { return r.ProvideValue(sp); } catch (ArgumentNullException ex) when (ex.ParamName == "serviceProvider") { throw new InvalidOperationException("x:Reference must be evaluated within a XAML service context."); } Prevention
- Never invoke ProvideValue without the XAML runtime's provider.
- In tests, supply a provider returning IXamlNameResolver.
- Let the XAML object writer evaluate x:Reference.
When it happens
Trigger: Calling Reference.ProvideValue(null) directly, e.g. in a unit test or code path that bypasses the XAML runtime supplying the provider.
Common situations: Unit tests invoking ProvideValue without a provider, custom hosting that drops the service context, or misuse of the extension outside markup evaluation.
Related errors
- elements
- arrayType
- serviceProvider
- Name property is not set
- Type property must be set before calling ProvideValue method
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/a258ae3418f6d5d3.
Report an issue: GitHub.