Skip to content
Latchkey

ASP.NET Core "Unable to resolve service for type ... while attempting to activate" in CI

The ASP.NET Core container tried to construct a controller or service and could not supply one of its constructor parameters because that type was never registered. Register the missing service in Program.cs.

What this error means

A request or an integration test fails at activation time with "Unable to resolve service for type 'X' while attempting to activate 'Y'". It often only surfaces in CI because a test host builds the full container.

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

Diagnose it: SDK version and restore first

Most .NET CI failures are an SDK mismatch or a restore that did not happen. global.json pins the SDK, and if the runner does not have that exact version the failure message is about the project rather than the SDK.

Terminal
dotnet --info
cat global.json 2>/dev/null

# restore explicitly so a restore failure is not reported as a build failure
dotnet restore --verbosity normal
dotnet build --no-restore -warnaserror

Common causes

The dependency was never registered

A controller or handler takes IEmailSender, but no AddScoped/AddSingleton/AddTransient call registers an implementation, so the container cannot build it.

Registration lives in a code path CI does not hit

The registration is behind an environment check or a module that runs only outside CI, so under the test host the service is absent.

How to fix it

Register the service in the container

  1. Read the type inside "Unable to resolve service for type" - that is the missing registration.
  2. Add an AddScoped/AddSingleton/AddTransient for it in Program.cs (or in a shared service-registration method).
  3. Ensure the registration runs in every environment, including the test host.
Program.cs
builder.Services.AddScoped<IEmailSender, SmtpEmailSender>();

Override the dependency in the test host

For integration tests, register a fake in ConfigureTestServices so the container can activate the controller without external systems.

CustomWebApplicationFactory.cs
builder.ConfigureTestServices(services =>
{
    services.AddScoped<IEmailSender, FakeEmailSender>();
});

How to prevent it

  • Register every injected dependency in a single service-registration extension shared by app and tests.
  • Avoid environment-gated registrations that skip CI.
  • Add a startup validation test that builds the service provider and catches missing registrations.

Frequently asked questions

What causes ASP.NET core "Unable to resolve service for type ... while attempting to activate" in CI?
There are 2 common causes: the dependency was never registered and registration lives in a code path ci does not hit. A controller or handler takes IEmailSender, but no AddScoped/AddSingleton/AddTransient call registers an implementation, so the container cannot build it.
How do I fix ASP.NET core "Unable to resolve service for type ... while attempting to activate" in CI?
There are 2 fixes depending on which cause you have: register the service in the container and override the dependency in the test host. Work through them in order, since the first is the most common.
What does ASP.NET core "Unable to resolve service for type ... while attempting to activate" in CI actually mean?
A request or an integration test fails at activation time with "Unable to resolve service for type 'X' while attempting to activate 'Y'".
How do I stop ASP.NET core "Unable to resolve service for type ... while attempting to activate" in CI happening again?
Register every injected dependency in a single service-registration extension shared by app and tests. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card