The following refers to a .NET Core application with dependencies as follows...
Microsoft.NETCore.App
Microsoft.AspNet.WepApi.Client (5.2.7)
At Microsoft.com is the document Call a Web API From a .NET Client (C#)
from 2017 November.
Link... https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
Within the document is this client side invocation of HTTP GET.
static HttpClient client = new HttpClient();
static async Task<Product> GetProductAsync(string path)
{
Product product = null;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync<Product>();
}
return product;
}
The value response.Content
refers to an HttpContent
object. As of 2020 July HttpContent
has no instance method with the signature ReadAsAsync<T>()
, at least according to the following document. However, this instance method works.
Reference link to where there is no instance method with the signature ReadAsAsync<T>()
...
https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent?view=netcore-3.1
There is a static method HttpContentExtensions.ReadAsAsync<T>(myContent)
where myContent
refers to an HttpContent
object. This static method also works.
Reference link... https://learn.microsoft.com/en-us/previous-versions/aspnet/hh834253(v=vs.118)
For example one documented signature has the...
static icon followed by
ReadAsAsync<T>(HttpContent)
and a description that says it will return Task<T>
. This static method is probably the behind the scenes implementation of the instance method.
However there is information at the top of the static method webpage that indicates... "We're no longer updating this content regularly. Check the Microsoft Product Lifecycle for information about how this product, service, technology, or API is supported."
Has HttpContent.ReadAsAsync<T>()
of both forms, instance and static, been superceded in .NET Core 3.1?
The other answers are not correct.
The method ReadAsAsync is part of the System.Net.Http.Formatting.dll
Which in turn is part of the nuget: Microsoft.AspNet.WebApi.Client
I just created a new Console project .Net Core 3.1 and added 2 nugets
I created a project with .NET Core 3.1 here are some pictures:
Here is my project file:
Here is the code I just wrote which compiles just fine:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace Custom.ApiClient
{
internal static class WebApiManager
{
//private const string _requestHeaderBearer = "Bearer";
private const string _responseFormat = "application/json";
private static readonly HttpClient _client;
static WebApiManager()
{
// Setup the client.
_client = new HttpClient { BaseAddress = new Uri("api url goes here"), Timeout = new TimeSpan(0, 0, 0, 0, -1) };
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_responseFormat));
// Add the API Bearer token identifier for this application.
//_client.DefaultRequestHeaders.Add(RequestHeaderBearer, ConfigHelper.ApiBearerToken);
}
public static async Task<T> Get<T>()
{
var response = _client.GetAsync("api extra path and query params go here");
return await ProcessResponse<T>(response);
}
private static async Task<T> ProcessResponse<T>(Task<HttpResponseMessage> responseTask)
{
var httpResponse = await responseTask;
if(!httpResponse.IsSuccessStatusCode)
throw new HttpRequestException(httpResponse.ToString());
var dataResult = await httpResponse.Content.ReadAsAsync<T>();
return dataResult;
}
}
}
UPDATE:
To clear some confusion about the dependecies for package Microsoft.AspNet.WebApi.Client
Here is a picture of the dependecies showing as of 2020-10-27 the dependencies which clearly shows it depends on Newtonsoft JSON 10 or higher. As of today there is no replacement of ReadAsAsync using System.Text.Json... So you can use ApiClient + Newtonsoft Json or create your own using System.Text.Json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With