Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Node.js, PHP and APIs · Troubleshooting walkthrough

ASP.NET Core cannot create a class because a constructor type is not registered

Short answer

'Unable to resolve service for type X while attempting to activate Y' means the constructor of Y needs X and nothing registered X. Usually X is missing from Program.cs, was registered as its class while Y wants the interface, or is a DbContext without AddDbContext. Register X under the exact type Y requests. Applies to .NET 10 and 11.

  • Applies to .NET 10.0 (LTS) and .NET 11.0, ASP.NET Core 10 and 11, Entity Framework Core 10 and 11
  • Updated
  • By Maaz Zindani, Senior Software Engineer

"Unable to resolve service for type X while attempting to activate Y" means the constructor of Y asks for X and the built-in container has nothing registered under that type. ASP.NET Core only creates what it has been told about, normally in Program.cs, so writing the class and its interface is not enough.

The message names two types, and they play different roles: the first is missing from the container, the second is the class whose constructor wants it. Search for the first name and read what you find.

The text you see

  • System.InvalidOperationException: Unable to resolve service for type 'MyApp.Services.IEmailSender' while attempting to activate 'MyApp.Controllers.AccountController'.
  • Unable to resolve service for type 'System.String' while attempting to activate 'MyApp.Services.SmtpSender'.

Stage 1

Search for the missing type and read the result

No registration line at all points to the first case below. A registration of a different type points to the second, a DbContext to the third, and a plain type such as System.String to the fourth.

Every mention of the missing type

grep -rn "IEmailSender" --include=*.cs .

Stage 2

Nothing registers the service

If the search finds the interface, its class and the constructor that uses it but no builder.Services.Add line, register it with a lifetime. AddScoped gives one instance per request in an MVC or Razor Pages app (per circuit in Blazor), which suits services that touch a database or the current user.

Registration before Build()

// Program.cs
builder.Services.AddScoped<IEmailSender, EmailSender>();

var app = builder.Build();

Stage 3

Registered as the class, requested as the interface

Microsoft's DI documentation states that registering with only an implementation type is the same as registering it with that class as the service type. The container knows EmailSender, but nothing is registered under IEmailSender, so the constructor's request has no match.

Two type arguments map interface to class

// before: builder.Services.AddScoped<EmailSender>();
builder.Services.AddScoped<IEmailSender, EmailSender>();

If other code asks for the concrete class too, keep both lines.

Stage 4

DbContext without AddDbContext or the options constructor

EF Core enters the container through AddDbContext, which registers the context as a scoped service. The context class also needs a public constructor that takes DbContextOptions<YourContext>, because that is how the configuration reaches it. Missing either piece leaves the container unable to build the context or the class that needs it.

Registration and the options constructor

// Program.cs
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

// AppDbContext.cs
public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
}

Stage 5

A string or number in the constructor

The .NET DI rules allow constructor arguments that DI does not provide only when those arguments have default values. A required string apiKey in a registered service therefore stops activation with this error. Bind the value to configuration and inject the options instead.

Options pattern in place of a raw string

// Program.cs
builder.Services.Configure<SmtpOptions>(builder.Configuration.GetSection("Smtp"));
builder.Services.AddScoped<SmtpSender>();

// SmtpSender.cs
public class SmtpSender(IOptions<SmtpOptions> options)
{
    private readonly SmtpOptions _smtp = options.Value;
}

Stage 6

Start the app and open the page that threw

It should load with no exception in the console. For a lasting guard, add a test that builds the host and resolves each controller or key service once, so a missing registration fails in CI instead of on the first request.

If the message instead says a scoped service cannot be consumed from a singleton, the type is registered but with a lifetime that does not fit. That is a separate fix.

Run it

dotnet run

If you only keep one thing

  • The first type in the message is missing from the container; the second is the class that wanted it.
  • Register under the exact type the constructor requests.
  • Pass settings through IOptions, not raw strings.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want a second pair of eyes on it?

Send me the error and what changed before it started. I reply within 24 hours.