IServiceCollection does not contain a defintion for AddHttpClient

C#.NetHttpclientasp.net Core-2.0

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

You need to install Microsoft.Extensions.Http from NuGet

Solution 2 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAskView Question on Stackoverflow
Solution 1 - C#Rahul JainView Answer on Stackoverflow
Solution 2 - C#AskView Answer on Stackoverflow