DI013

Implementation Type Mismatch

Invalid service/implementation pairs that compile but fail at runtime, including generic, typeof(...), keyed, named-argument, and ServiceDescriptor registrations. Closed-type compatibility follows CLR assignability — identity, reference, boxing, and TNullable<T> — so implicit numeric conversions and user-defined operators are not treated as a valid implementation binding.

Error Default severity · Code fix: Yes

Why it matters

Service activation throws at runtime (ArgumentException/InvalidOperationException depending on path).

A round plug will not fit a square socket just because both are on your desk.

README problem example

public interface IRepository { }
public sealed class WrongType { }

services.AddSingleton(typeof(IRepository), typeof(WrongType));

README better pattern

public sealed class SqlRepository : IRepository { }
services.AddSingleton(typeof(IRepository), typeof(SqlRepository));

Yes. Offers broad assists where the syntax and symbols are local enough to rewrite safely: remove the invalid block-contained standalone registration, replace the implementation type with a compatible candidate, or retarget the service type to an interface/base type implemented by the current implementation, including invalid implementation-instance registrations. Embedded single-line statement bodies stay manual unless a symbol-backed type rewrite is available. Candidate suggestions never include generic type definitions or structs — both produce registrations that fail to compile or crash at resolution.

Repo sample extraction

Examples pulled from the sample app

Open full sample file

Sample app safe pattern

        // ✅ GOOD: Correct implementation type
        services.AddSingleton(typeof(IRepository), typeof(SqlRepository));

        // ⚠️ BAD: WrongType does not implement IRepository
        // This will throw an ArgumentException at runtime
        services.AddSingleton(typeof(IRepository), typeof(WrongType));

Sample app warning case

        // ⚠️ BAD: WrongType does not implement IRepository
        // This will throw an ArgumentException at runtime
        services.AddSingleton(typeof(IRepository), typeof(WrongType));
        
        // ⚠️ BAD: String does not implement IRepository
        services.AddScoped(typeof(IRepository), typeof(string));
    }
}

Nearby diagnostics

Other rules in this family

All 37 rules