Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project, I am calling webapi services - Get action from controller of another project. My code is:

using System.Net.Http;
using System.Configuration;
using System.Net.Http.Headers;


public ActionResult Index()
       {
           ViewBag.Place = new SelectList(GetPlaces(), "ID", "PlaceName");

           FlightDetails flightDetails = new FlightDetails();
           flightDetails.DepartureDate = System.DateTime.Now;
           flightDetails.ReturnDate = System.DateTime.Now;
           return View(flightDetails);
       }


private List<tblPlace> GetPlaces()
{
    List<tblPlace> lstPlaces = new List<tblPlace>(); //to be uncommented

    //lstPlaces = TwentyFour7Sewa.Utils.APIEngine<tblPlace>.GetAll("api/fromto");
    using (var client = new HttpClient())
    {
        string apiHost = ConfigurationManager.AppSettings["ApiHost"];
        string apiDirectory = "api/fromto";

        List<tblPlace> result = new List<tblPlace>();
        client.BaseAddress = new Uri(apiHost);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = client.GetAsync(apiDirectory).Result;

        if (response.IsSuccessStatusCode)
        {
            result = response.Content.ReadAsAsync<List<tblPlace>>().Result;
            return result;
        }
        else
        {
            return null;
        }
    }

    //lstPlaces = lstPlaces.OrderBy(o => o.PlaceName).ToList<tblPlace>();
    //return lstPlaces;
}


However, in line
ViewBag.Place = new SelectList(GetPlaces(), "ID", "PlaceName");
in Index() actionresult, I am getting error,
System.MissingMethodException: 'Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Net.Http.HttpContentExtensions.ReadAsAsync(System.Net.Http.HttpContent)'.'


Web api is returning required value. The issue is with line
ReadAsAsync
. I did nothing on this part of code. It was working fine till yesterday. All of a sudden what happened, I have no clue.

What I have tried:

Found somewhere that I need to use Microsoft.Net.Http instead of System.Net.Http since this is obsolete. When I installed
Microsoft.Net.Http 
from nuget package in this project, this dll was not shown in project reference.
Posted
Updated 5-Mar-19 7:28am
Comments
Bryian Tan 2-Mar-19 13:54pm    
Have you try convert the method into Async?
Codes DeCodes 3-Mar-19 11:21am    
Yes, did it. But same exception..

public static async Task<List<tblusertype>> GetAll()
{
string apiHost = ConfigurationManager.AppSettings["ApiHost"];
using (var client = new HttpClient())
{
List<tblusertype> result = new List<tblusertype>();
client.BaseAddress = new Uri(apiHost);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.GetAsync("api/UserType");

if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsAsync<List<tblusertype>>().Result;
//return Task.FromResult<tblusertype>(result);
return null;
}
else
{
return null;
}
}
}

1 solution

That method is defined in the System.Net.Http.Formatting assembly, which is distributed via NuGet. Confusingly, the NuGet package name is Microsoft.AspNet.WebApi.Client:

NuGet Gallery | Microsoft.AspNet.WebApi.Client 5.2.7[^]

NB: The async conversion of your method should not call .Result at all:
C#
public static async Task<List<tblusertype>> GetAll()
{
    string apiHost = ConfigurationManager.AppSettings["ApiHost"];
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(apiHost);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        
        using (var response = await client.GetAsync("api/UserType"))
        {
            if (!response.IsSuccessStatusCode) return null;
            return await response.Content.ReadAsAsync<List<tblusertype>>();
        }
    }
}

Also: You're using HttpClient wrong and it is destabilizing your software | ASP.NET Monsters[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900