dotnet/maui · error · InvalidCastException

Resolved instance is not of the correct type.

Error message

Resolved instance is not of the correct type.

What it means

DependencyResolver.Resolve invokes the registered Resolver func and, if it returns a non-null object, checks type.IsInstanceOfType(result). When the returned object is not assignable to the requested type it throws InvalidCastException. This guards DI resolution so callers get a type-safe instance or null.

Source

Thrown at src/Controls/src/Core/DependencyResolver.cs:37

			Resolver = resolver;
		}

		/// <summary>Sets a resolver function that takes only a type.</summary>
		/// <param name="resolver">The resolver function.</param>
		public static void ResolveUsing(Func<Type, object> resolver)
		{
			Resolver = (type, objects) => resolver.Invoke(type);
		}

		internal static object Resolve(Type type, params object[] args)
		{
			var result = Resolver?.Invoke(type, args);

			if (result != null)
			{
				if (!type.IsInstanceOfType(result))
				{
					throw new InvalidCastException("Resolved instance is not of the correct type.");
				}
			}

			return result;
		}

		internal static object ResolveOrCreate(
			[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type)
				=> ResolveOrCreate(type, null, null);

		internal static object ResolveOrCreate(
			[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type,
			object source,
			Type visualType,
			params object[] args)
		{
			visualType = visualType ?? _defaultVisualType;

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Fix the resolver to return an instance assignable to the requested type (return null instead of a wrong type if unavailable).
  2. Verify the registration maps the interface/base type to a compatible implementation.
  3. Return null from the resolver when the type cannot be satisfied, so Resolve returns null instead of throwing.

Example fix

// before
DependencyResolver.Register(t => t == typeof(IMyService) ? new WrongService() : null);
// WrongService does not implement IMyService -> InvalidCastException

// after
DependencyResolver.Register(t => t == typeof(IMyService) ? (object)new MyService() : null);
Defensive patterns

Strategy: validation

Validate before calling

// After resolving, assert type compatibility yourself (Resolve already does, but catch earlier).
object resolved = myResolver(type, args);
if (resolved is not null && !type.IsInstanceOfType(resolved))
    throw new InvalidCastException($"Resolver returned {resolved.GetType()} for {type}.");

Type guard

static bool ResolvesTo(Type requested, object instance) => instance is null || requested.IsInstanceOfType(instance);

Try / catch

try { return DependencyResolver.Resolve(typeof(TService)); }
catch (InvalidCastException) { /* log resolver misconfiguration */ return null; }

Prevention

When it happens

Trigger: Registering a DependencyResolver.Register(handler) whose handler returns an object of the wrong type for the requested Type; a misconfigured DI container mapping a service to an incompatible implementation.

Common situations: Custom DI integration returning a concrete type that does not implement/derive from the requested abstraction; stale registrations after refactoring an interface.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/f04ec5a9f0aa9df4. Report an issue: GitHub.