DI018

Non-Instantiable Implementation Type

registrations whose implementation type cannot be constructed by the DI container, such as abstract classes, interfaces, static classes, delegate types registered without a factory, or concrete classes with no public constructors.

Default severity: Warning · Code fix: No

Why it matters

these registrations compile, but fail at runtime when the container tries to activate the service.

Writing a ghost on the class register does not mean someone can actually show up for class.

README problem example

public interface IMyService { }
public sealed class BadPrivateCtorService : IMyService
{
    private BadPrivateCtorService() { }
}

services.AddSingleton<IMyService, BadPrivateCtorService>();

README better pattern

public sealed class GoodConcreteService : IMyService { }

services.AddSingleton<IMyService, GoodConcreteService>();

// For delegate types, register with a factory expression:
services.AddSingleton<MyHandler>(sp => (msg) => Console.WriteLine(msg));

No.

Repo sample extraction

Examples pulled from the sample app

Open full sample file

Sample app non-instantiable type

public abstract class BadAbstractService : IMyService { }

Sample app safe pattern

public class GoodConcreteService : IMyService { }

Related guides

  • No problem-guide pages point here yet.