unoplatform/uno · error · ArgumentNullException

serviceProvider

Error message

serviceProvider

What it means

Thrown by TypeExtension.ProvideValue when serviceProvider is null and TypeName (not Type) is being resolved. The comment in source notes serviceProvider may legitimately be null only when Type is supplied; otherwise a resolver is required to turn the string into a Type.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Windows.Markup/TypeExtension.cs:81

		public Type Type { get; set; }
		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
		public string TypeName { get; set; }

		public override object ProvideValue (IServiceProvider serviceProvider)
		{
			if (Type != null)
			{
				return Type;
			}

			if (TypeName == null)
			{
				throw new InvalidOperationException ("Either TypeName or Type must be filled before calling ProvideValue method");
			}

			if (serviceProvider == null) // it can be null when Type is supplied.
			{
				throw new ArgumentNullException ("serviceProvider");
			}

			var p = serviceProvider.GetService (typeof (IXamlTypeResolver)) as IXamlTypeResolver;
			if (p == null)
			{
				throw new InvalidOperationException ("serviceProvider does not provide IXamlTypeResolver service.");
			}

			var ret = p.Resolve (TypeName);
			if (ret == null)
			{
				throw new InvalidOperationException (String.Format (CultureInfo.InvariantCulture, "Type '{0}' is not resolved as a valid type by the type resolver '{1}'.", TypeName, p.GetType ()));
			}

			return ret;
		}
	}
}

View on GitHub (pinned to 0418340488)

Solutions

  1. Always pass a non-null IServiceProvider that advertises IXamlTypeResolver when TypeName-based resolution is needed.
  2. Prefer setting the Type property directly so the serviceProvider argument is not required.
  3. Guard: if Type is null, ensure serviceProvider is non-null before calling ProvideValue.

Example fix

// before
var ext = new TypeExtension { TypeName = "local:MyControl" };
var v = ext.ProvideValue(null); // throws

// after
var ext = new TypeExtension { TypeName = "local:MyControl" };
var v = ext.ProvideValue(serviceProviderWithTypeResolver);
Defensive patterns

Strategy: validation

Validate before calling

// When using TypeName resolution, provider must be non-null
var ext = new TypeExtension { TypeName = "local:MyControl" };
if (ext.Type == null && sp == null)
    throw new ArgumentNullException(nameof(sp));
var value = ext.ProvideValue(sp);

Type guard

static bool SafeToProvideValue(TypeExtension e, IServiceProvider sp)
    => e.Type != null || sp != null;

Prevention

When it happens

Trigger: Calling ProvideValue(null) when Type is null and TypeName is set; manually invoking the extension outside a XAML context.

Common situations: Unit tests or tooling that call ProvideValue without provisioning services; refactors that dropped the service provider argument.

Related errors


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