locked
HttpConent does not contain a definition for ReadAsAsync and no accessible extension method ReadAsAync accepting a first argument of type HttpContent could be found  RRS feed

  • Question

  • User-973886032 posted

    hi guys

    I have a web API project that uses a HTTPRequest class to get a remote JSON result file and pass to the calling app. However I want to encapsulate the functionality of the request in an a class Project and make references to both the webAPI Project and main website, using a class, but it keeps giving errors. 

    Here is my working code.  WebAPI Project ( This works fine )

    private const string URL = "test"; 
    [HttpGet] public async Task<ActionResult<IEnumerable<ORDERS>>> GetORDERS() { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(URL); // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); // List data response. HttpResponseMessage response = client.GetAsync(urlParameters).Result; // Blocking call! Program will wait here until a response is received or a timeout occurs. if (response.IsSuccessStatusCode) { // Parse the response body. var dataObjects = response.Content.ReadAsAsync<IEnumerable<DataObject>>().Result; //Make sure to add a reference to System.Net.Http.Formatting.dll foreach (var d in dataObjects) { var x = ("{0}", "MMT"); } } else { var x = ("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } return await _context.ORDERS.ToListAsync(); }

    However when I add it to a class, I get the error

    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'HttpContent' does not contain a definition for 'ReadAsAsync' and no accessible extension method 'ReadAsAsync' accepting a first argument of type 'HttpContent' could be found (are you missing a using directive or an assembly reference?)	NetWealth.Services	C:\****GetTodaysRates.cs	73	Active
    

    HttpConent does not contain a definition for ReadAsAsync and no accessible extension method ReadAsAync accepting a first argument of type HttpContent could be found 

    Saturday, May 29, 2021 9:44 AM

Answers

  • User475983607 posted

    However when I add it to a class, I get the error

    Severity Code Description Project File Line Suppression State Error CS1061 'HttpContent' does not contain a definition for 'ReadAsAsync' and no accessible extension method 'ReadAsAsync' accepting a first argument of type 'HttpContent' could be found (are you missing a using directive or an assembly reference?) NetWealth.Services C:\****GetTodaysRates.cs 73 Active

    The class is missing a reference to the legacy class; System.Net.Http.Formatting.

    You should use Microsoft.AspNet.WebApi.Client with .NET Core.

    https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-5.0

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 29, 2021 10:38 AM

All replies

  • User475983607 posted

    However when I add it to a class, I get the error

    Severity Code Description Project File Line Suppression State Error CS1061 'HttpContent' does not contain a definition for 'ReadAsAsync' and no accessible extension method 'ReadAsAsync' accepting a first argument of type 'HttpContent' could be found (are you missing a using directive or an assembly reference?) NetWealth.Services C:\****GetTodaysRates.cs 73 Active

    The class is missing a reference to the legacy class; System.Net.Http.Formatting.

    You should use Microsoft.AspNet.WebApi.Client with .NET Core.

    https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-5.0

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 29, 2021 10:38 AM
  • User-973886032 posted

    thank you

    sometimes these nuget packages do my head in

    Saturday, May 29, 2021 10:16 PM