IServiceCollection does not contain a defintion for AddHttpClient
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
When the compiler reports that IServiceCollection does not contain a definition for AddHttpClient, the fix is almost always one of two things: add using Microsoft.Extensions.DependencyInjection; to the source file, or add a reference to the Microsoft.Extensions.Http NuGet package in the project that contains the call. AddHttpClient is an extension method, so the compiler can only discover it when both the namespace import and the assembly reference are present.
Why This Error Is Misleading
Extension methods in C# look like regular instance methods when you call them, but the compiler resolves them differently. An extension method is a static method in a static class, discoverable only when the namespace containing that class is imported via a using directive.
AddHttpClient is defined in the Microsoft.Extensions.DependencyInjection.HttpClientFactoryServiceCollectionExtensions class, which lives in the Microsoft.Extensions.Http assembly. When the compiler cannot find this extension, it reports the error as if IServiceCollection itself is missing the method, which obscures the real cause.
The second half of this error message hints at the actual problem: "no accessible extension method could be found."
Fix 1: Add the Using Directive
The most common fix. The source file that calls AddHttpClient must import the namespace:
This single line resolves the error in the majority of cases because ASP.NET Core web projects typically already reference the Microsoft.Extensions.Http assembly through the shared framework.
Before
After
Fix 2: Add the NuGet Package Reference
If adding the using directive does not resolve the error, the project is missing a reference to the assembly that contains the extension method.
For .NET CLI
For the .csproj file
For Package Manager Console (Visual Studio)
After adding the package, restore dependencies and rebuild:
When Each Fix Applies
| Project Type | Namespace Import Needed? | Package Reference Needed? |
| ASP.NET Core Web App | Yes | No (included via shared framework) |
| ASP.NET Core Web API | Yes | No (included via shared framework) |
| Class Library targeting .NET 8+ | Yes | Yes |
| Console Application | Yes | Yes |
| Worker Service | Yes | Usually no (included in template) |
| Blazor Server | Yes | No (included via shared framework) |
| xUnit / NUnit Test Project | Yes | Yes |
ASP.NET Core web projects reference the Microsoft.AspNetCore.App shared framework, which includes Microsoft.Extensions.Http. Class libraries, console apps, and test projects do not, so they need an explicit package reference.
The Multi-Project Trap
This is the scenario that catches experienced developers. Consider a solution with this structure:
If ServiceRegistration.cs in the class library calls AddHttpClient, the build fails even though the web project has the shared framework reference. Compilation happens at the project level. The class library needs its own reference:
This is the most common cause of the error in real-world codebases with clean architecture or layered project structures.
Verifying the Fix
A minimal program that compiles confirms the extension method is available:
If this compiles, the issue in your application is project-specific, not environmental.
Named vs Typed Clients
Once the error is resolved, you have two main patterns for registering HTTP clients.
Named Clients
Named clients are identified by a string key:
Consumers request a specific client by name through IHttpClientFactory:
Typed Clients
Typed clients bind configuration to a specific class:
Typed clients are injected directly, without needing IHttpClientFactory:
Comparison
| Feature | Named Clients | Typed Clients |
| Registration | String key | Class type |
| Injection | Via IHttpClientFactory | Direct injection |
| Compile-time safety | No (string key) | Yes (type checked) |
| Configuration co-location | Separate from usage | Next to the client class |
| Best for | Many simple endpoints | Dedicated service clients |
Adding Delegating Handlers
A common follow-up after registering HTTP clients is adding cross-cutting concerns like logging, retry, or authentication:
These methods are also extension methods from the same Microsoft.Extensions.Http package, so they become available once the package reference and namespace import are in place.
Common Pitfalls
- Missing
using Microsoft.Extensions.DependencyInjection;in the source file. This is the single most common cause. The package may be installed, but without the namespace import the compiler cannot discover the extension method. - Adding the package to the wrong project in a multi-project solution. The package reference must exist in the
.csprojof the project that contains theAddHttpClientcall, not just in the startup project. - Confusing the shared framework with explicit packages. ASP.NET Core web projects include
Microsoft.Extensions.Httpthrough the shared framework. Class libraries and console apps do not. - Version mismatches across projects. If one project references
Microsoft.Extensions.Http8.0.0 and another references 6.0.0, binding redirects or runtime errors can occur. Align versions across the solution. - Forgetting to restore packages after adding the reference. The package is not usable until
dotnet restoreruns. Most IDEs do this automatically, but CI pipelines may not. - Using
new HttpClient()directly instead ofIHttpClientFactory. This bypasses handler lifetime management and can lead to socket exhaustion under load. If you are fixing this error, commit to using the factory pattern properly.
Summary
AddHttpClientis an extension method defined inMicrosoft.Extensions.Http, not a built-in member ofIServiceCollection.- Add
using Microsoft.Extensions.DependencyInjection;to the source file as the first fix. - Add a
PackageReferencetoMicrosoft.Extensions.Httpif the project is a class library, console app, or test project. - In multi-project solutions, the package reference must exist in the project that contains the
AddHttpClientcall. - ASP.NET Core web projects inherit the package through the shared framework and typically only need the
usingdirective. - Once the error is resolved, choose between named clients (string-keyed) and typed clients (class-based) depending on your architecture.
Related reading
- IUnityContainer.ResolveT throws error claiming it cannot be used with type parameters
- Java Constructor Inheritance
- Java generics T vs Object
- Java Generics With a Class & an Interface - Together
- ISO 9797-1 Algorithm 1 CBC-MAC in C
- Issues with Accord.NET SVM classification task
- Java Instanceof and Generics
- Java Multiple Inheritance

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.