9

Is it somehow possible to make a Web api that calls another web api?

I am using the code below to access a web api from my web api, but it never return from the call. If I use the code from a console app, it is working fine.

public void DoStuff(){
    RunAsync().Wait();
}

public static async Task RunAsync(){
using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:53452/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // HTTP GET
    HttpResponseMessage response = await client.GetAsync("umbraco/api/Member/Get?username=test");
    if (response.IsSuccessStatusCode)
    {
        string user = await response.Content.ReadAsAsync<string>();
    }

}
PNR
  • 555
  • 3
  • 12
  • 26
  • Is it an umbraco API you are trying to call in your web API? – Jayaraj.K Jun 17 '15 at 12:11
  • 2
    Show the full method declaration and how you're calling it (the async method). – Yuval Itzchakov Jun 17 '15 at 12:13
  • Yes I am calling an function on a Umbraco API (My own function). – PNR Jun 17 '15 at 12:13
  • Which version of Umbraco are you using ? Can you send me the details? – Jayaraj.K Jun 17 '15 at 12:15
  • I agree with @YuvalItzchakov. Can you provide the entire action method. Also just to bypass the obvious you have set a breakpoint just before and after the GetAsync() to verify execution is both getting to the call and not returning (instead of some other unrelated issue). – Gerald Davis Jun 17 '15 at 12:18
  • I am using Umbraco version 7.2.4, but I don't think the problem relies to Umbraco. The function in Umbraco api is working fine from Fiddler. – PNR Jun 17 '15 at 12:20
  • I have updated the code :-) – PNR Jun 17 '15 at 12:29
  • The working code isn't useful. The full method for the unworking code is where the problem lies. – Gerald Davis Jun 17 '15 at 14:23
  • I was starting making a test setup, to find out if it was working or not, so the code is very simple (sorry). Now I have tried using this example: http://www.codeproject.com/Tips/497123/How-to-make-REST-requests-with-Csharp, and it is working fine. It is as something is locking when using the code above from a web API function. – PNR Jun 18 '15 at 10:28

2 Answers2

7

I also went through the same problem, after much research I discovered that the await operator does not stop the work if the HttpClient returns error 500.
To work around the problem I used Task.Wait().

var response = client.GetAsync ("umbraco/api/Member/Get?username=test");
response.Wait ();

I hope this helps others.

Propeus
  • 378
  • 3
  • 7
0

Yes you can make a call to a remote web api within the action method of a web api controller.

Lets eliminate the obvious first.

If you set a breakpoint at the start of this action method it is getting hit right? If not then the issue lies in the routing not the action method.

If you set a breakpoint at the if statement does it get hit or is the client.GetAsync() call never returning?

If you haven't done already you may wish to use a tool like fiddler (http://www.telerik.com/fiddler) to compare the request & response from a working use of the api and this broken one. I know you said it is identical to a working implementation but I have found fiddler invaluable to verify exactly what is being sent "on the wire".

Gerald Davis
  • 4,541
  • 2
  • 31
  • 47
  • I am using Fiddler, but it never makes the call to the other API, so Fiddler shows only the first call to the "first" api. When debugging it just never get back from "await client.GetAsync("umbraco/api/Member/Get?username=test");", no errors – PNR Jun 17 '15 at 12:17
  • Ok well that is something. If fiddler shows nothing on the wire it isn't just that no response is coming back, no request is being made. Can you provide the entire action method as the issue probably lies "upstream" of the code you provided. The short answer is yes the code provided should work. The action method of an api controller is just a method. Execution inside it should be no different than execution inside another method (i.e. your console test application). – Gerald Davis Jun 17 '15 at 12:21
  • I had the same issue when I tried calling an Umbraco API, http://stackoverflow.com/questions/24508306/umbraco-ajax-call-not-working. I do not know if this is the case for you. – Jayaraj.K Jun 17 '15 at 12:24
  • The Umbraco api is working fine from Fiddler, but it looks like something is getting locked in the API from where I call the Umbraco API. – PNR Jun 17 '15 at 12:31
  • When I use the code from the exaple below it is working fine. But I haven't still not found out why the other example is not working: http://www.codeproject.com/Tips/497123/How-to-make-REST-requests-with-Csharp – PNR Jun 22 '15 at 06:36