IServiceCollection does not contain a definition for AddHttpClient
Codemia Knowledge Hub ·
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 only finds it when both the namespace import and the assembly reference are present.
Why the error message is misleading
Extension methods look like instance methods at the call site, but the compiler resolves them differently. An extension method is a static method in a static class, and it is discoverable only when that class's namespace is imported with a using directive.
AddHttpClient is defined in Microsoft.Extensions.DependencyInjection.HttpClientFactoryServiceCollectionExtensions, which lives in the Microsoft.Extensions.Http assembly. When the compiler cannot find the extension, it reports the error as if IServiceCollection itself lacked the method:
The second half of the message points at the real problem: "no accessible extension method could be found."
Fix 1: Add the using directive
This is the most common fix. The source file that calls AddHttpClient must import the namespace:
That single line fixes most cases, because ASP.NET Core web projects already reference the Microsoft.Extensions.Http assembly through the shared framework. Even with the package installed, the compiler cannot see the method without this import.
Before
After
Fix 2: Add the NuGet package reference
If the using directive does not fix it, the project is missing a reference to the assembly that contains the extension method.
.NET CLI
.csproj file
Package Manager Console (Visual Studio)
Then restore dependencies and rebuild:
The package is not usable until dotnet restore runs. Most IDEs restore automatically, but CI pipelines may not.
Which fix each project type needs
| 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 catches experienced developers. Consider a solution like this:
If ServiceRegistration.cs in the class library calls AddHttpClient, the build fails even though the web project has the shared framework reference. Compilation happens per project, so the class library needs its own reference, not just the startup project:
This is the most common cause of the error in real codebases with clean architecture or layered project structures.
Also keep Microsoft.Extensions.Http versions aligned across the solution. If one project references 8.0.0 and another 6.0.0, binding redirects or runtime errors can occur.
Verify the fix with a minimal program
If this minimal program compiles, the extension method is available and the problem in your application is project-specific, not environmental:
Named vs typed clients
Once the error is gone, there are two main ways to register HTTP clients. Either is better than calling new HttpClient() directly, which bypasses handler lifetime management and can exhaust sockets under load.
Named clients
Named clients are identified by a string key:
Consumers request a client by name through IHttpClientFactory:
Typed clients
Typed clients bind configuration to a specific class:
They are injected directly, without IHttpClientFactory:
| 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 next step is adding cross-cutting concerns such as logging, retry or authentication:
These methods are also extension methods from Microsoft.Extensions.Http, so they become available once the package reference and namespace import are in place.
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 courseObject-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.