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.
DI013
invalid `typeof` service/implementation pairs that compile but fail at runtime.
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.
Install
dotnet add package DependencyInjection.Lifetime.Analyzers --version 2.2.2
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));
Repo sample extraction
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));
}
}
Related guides
More documentation