Ask
Ask

Reputation: 3746

IServiceCollection does not contain a defintion for AddHttpClient

I am trying to use HttpClient in my .net core 2.0 project and for that I have injected HttpClient in my controller. But when I am trying to configure httpclient in my startup.cs I am getting this error: "IServiceCollection does not contain a defintion for AddHttpClient". I have already referenced using Microsoft.AspNetCore.Http; and using Microsoft.Extensions.DependencyInjection; and here is what I am trying to do:

services.AddHttpClient<TestController>();

It's working fine in other project with same namespaces but here I am getting the error. Any help?

Upvotes: 113

Views: 78970

Answers (4)

Bikash
Bikash

Reputation: 1

I resolved it by adding using Microsoft.Extensions.DependencyInjection; in .net 6.

Upvotes: 0

Samyush
Samyush

Reputation: 11

I went through the same error but by adding

builder.Services.AddHttpClient();

the error states "'IServiceCollection' does not contain a definition for 'AddHttpClient' and no accessible" and found out that with .net 6 we no longer need to add

builder.Services.AddHttpClient();

so if anyone comes across the same error, we just do not need to add the HttpClient

Upvotes: -6

Rahul Jain
Rahul Jain

Reputation: 3748

You need to install Microsoft.Extensions.Http from NuGet

Upvotes: 349

Ask
Ask

Reputation: 3746

Aah, I have found the solution. I think services.AddHttpClient work with .net core 2.1. So I updated my .net core version to 2.1 and updated the microsoft packages to 2.1 and it starts working.

Upvotes: 3

Related Questions