Click here to Skip to main content
14,366,827 members
Rate this:
Please Sign up or sign in to vote.
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 8: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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
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[^]
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100