unoplatform/uno · error · ArgumentNullException

serviceProvider

Error message

serviceProvider

What it means

ArgumentNullException thrown by the Bind markup extension's ProvideValue (System.Xaml, ported from Mono) when the IServiceProvider argument is null. Bind resolves a named target via IXamlNameResolver obtained from the service 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/Bind.cs:30

	public class Bind : MarkupExtension
	{
		public Bind()
		{
		}

		public Bind(string path)
		{
			Path = path;
		}

		[ConstructorArgument ("path")]
		public string Path { get; set; }

		public override object ProvideValue (IServiceProvider serviceProvider)
		{
			if (serviceProvider == null)
			{
				throw new ArgumentNullException ("serviceProvider");
			}

			if (Path == 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 (Path);
			if (ret == null)
			{
				ret = r.GetFixupToken (new string [] { Path }, true);
			}

View on GitHub (pinned to 0418340488)

Solutions

  1. Always pass the IServiceProvider supplied by the XAML runtime; never call ProvideValue manually without one.
  2. In tests, build a minimal service provider implementing GetService to return an IXamlNameResolver.
  3. Null-check the provider at the call site and fail with a clearer message before invoking.

Example fix

// before
var value = bind.ProvideValue(null);

// after
var value = bind.ProvideValue(xamlServiceProvider);
Defensive patterns

Strategy: validation

Validate before calling

static object SafeProvide(Bind b, IServiceProvider sp) { if (sp is null) throw new ArgumentNullException(nameof(sp)); return b.ProvideValue(sp); }

Type guard

static bool HasXamlServices(IServiceProvider sp) => sp?.GetService(typeof(IXamlNameResolver)) is not null;

Try / catch

try { return bind.ProvideValue(sp); } catch (ArgumentNullException ex) when (ex.ParamName == "serviceProvider") { throw new InvalidOperationException("Bind must be evaluated within a XAML service context."); }

Prevention

When it happens

Trigger: Calling Bind.ProvideValue(null) directly, e.g. in a unit test or code path that bypasses the XAML runtime which normally supplies the provider.

Common situations: Unit tests invoking ProvideValue without building a service provider, or custom hosting code that forgets to pass the XAML service context.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/bd037a78fa83d887. Report an issue: GitHub.