{"record":{"id":"f04ec5a9f0aa9df4","repo":"dotnet/maui","slug":"resolved-instance-is-not-of-the-correct-type","errorCode":null,"errorMessage":"Resolved instance is not of the correct type.","messagePattern":"Resolved instance is not of the correct type\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/DependencyResolver.cs","lineNumber":37,"sourceCode":"\t\t\tResolver = resolver;\n\t\t}\n\n\t\t/// <summary>Sets a resolver function that takes only a type.</summary>\n\t\t/// <param name=\"resolver\">The resolver function.</param>\n\t\tpublic static void ResolveUsing(Func<Type, object> resolver)\n\t\t{\n\t\t\tResolver = (type, objects) => resolver.Invoke(type);\n\t\t}\n\n\t\tinternal static object Resolve(Type type, params object[] args)\n\t\t{\n\t\t\tvar result = Resolver?.Invoke(type, args);\n\n\t\t\tif (result != null)\n\t\t\t{\n\t\t\t\tif (!type.IsInstanceOfType(result))\n\t\t\t\t{\n\t\t\t\t\tthrow new InvalidCastException(\"Resolved instance is not of the correct type.\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\n\t\tinternal static object ResolveOrCreate(\n\t\t\t[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type)\n\t\t\t\t=> ResolveOrCreate(type, null, null);\n\n\t\tinternal static object ResolveOrCreate(\n\t\t\t[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type,\n\t\t\tobject source,\n\t\t\tType visualType,\n\t\t\tparams object[] args)\n\t\t{\n\t\t\tvisualType = visualType ?? _defaultVisualType;\n","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/DependencyResolver.cs#L19-L55","documentation":"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.","triggerScenarios":"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.","commonSituations":"Custom DI integration returning a concrete type that does not implement/derive from the requested abstraction; stale registrations after refactoring an interface.","solutions":["Fix the resolver to return an instance assignable to the requested type (return null instead of a wrong type if unavailable).","Verify the registration maps the interface/base type to a compatible implementation.","Return null from the resolver when the type cannot be satisfied, so Resolve returns null instead of throwing."],"exampleFix":"// before\nDependencyResolver.Register(t => t == typeof(IMyService) ? new WrongService() : null);\n// WrongService does not implement IMyService -> InvalidCastException\n\n// after\nDependencyResolver.Register(t => t == typeof(IMyService) ? (object)new MyService() : null);","handlingStrategy":"validation","validationCode":"// After resolving, assert type compatibility yourself (Resolve already does, but catch earlier).\nobject resolved = myResolver(type, args);\nif (resolved is not null && !type.IsInstanceOfType(resolved))\n    throw new InvalidCastException($\"Resolver returned {resolved.GetType()} for {type}.\");","typeGuard":"static bool ResolvesTo(Type requested, object instance) => instance is null || requested.IsInstanceOfType(instance);","tryCatchPattern":"try { return DependencyResolver.Resolve(typeof(TService)); }\ncatch (InvalidCastException) { /* log resolver misconfiguration */ return null; }","preventionTips":["Make the resolver return null (not a wrong type) when it cannot satisfy a request.","Verify DI registrations map abstractions to compatible implementations.","After refactoring an interface, audit all DependencyResolver registrations."],"tags":["dependencyresolver","di","invalidcast","type-mismatch"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}