Mist
Mist

Reputation: 684

Web ApI: HttpClient is not invoking my web api action

See first how i design my web api action.

[System.Web.Http.RoutePrefix("api/Appointments")]
public class AppointmentsServiceController : ApiController
{

    [System.Web.Http.HttpGet, System.Web.Http.Route("UserAppointments/{email}")]
    public IHttpActionResult UserAppointments(string email)
    {
        if (!string.IsNullOrEmpty(email))
        {
            AppointmentsService _appservice = new AppointmentsService();
            IEnumerable<Entities.Appointments> app = _appservice.GetUserWiseAppointments(email);

            if (app.Count() <= 0)
            {
                return NotFound();
            }
            else
            {
                return Ok(app);
            }
        }
        else
        {
            return BadRequest();

        }

    }
}

Now this way i am calling web api action from my asp.net mvc action by HttpClient.

    public async Task<ActionResult> List()
    {

        var fullAddress = ConfigurationManager.AppSettings["baseAddress"] + "api/Appointments/UserAppointments/" + Session["useremail"];

        IEnumerable<Entities.Appointments> app = null;
        try
        {
            using (var client = new HttpClient())
            {
                using (var response = client.GetAsync(fullAddress).Result)
                {
                    if (response.IsSuccessStatusCode)
                    {
                        var customerJsonString = await  response.Content.ReadAsStringAsync();
                        app = JsonConvert.DeserializeObject<IEnumerable<Entities.Appointments>>(customerJsonString);
                    }
                    else
                    {
                        Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                        var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);
                        //MessageBox.Show(dict["Message"]);
                    }
                }
            }
        }
        catch (HttpRequestException ex)
        {
            // catch any exception here
        }

        return View();
    }
 }

i want capture the return IEnumerable and if not data return that also i have to capture. please show me the right direction.

Where i made the mistake. thanks

Upvotes: 1

Views: 49

Answers (2)

Nkosi
Nkosi

Reputation: 247531

Mixing async with blocking calls like .Result

var response = client.GetAsync(fullAddress).Result

And

response.Content.ReadAsStringAsync().Result

can lead to deadlocks, which is possibly why it it not hitting your API.

Refactor the code to be async all the way.

That would mean updating the using to

var response = await client.GetAsync(fullAddress)

and the reading of the content in the else statement to

await response.Content.ReadAsStringAsync()

Reference Async/Await - Best Practices in Asynchronous Programming

Upvotes: 2

nastraw
nastraw

Reputation: 21

It looks like you are not awaiting your GetAsync call, so in the following if (response.IsSuccessStatusCode) is probably always returning false. Try calling your method like this:

using (var response = (await client.GetAsync(fullAddress)).Result)
{

Upvotes: 1

Related Questions