HttpClient SendAsync from ASP .NET Core blocking calls down stack in Web API 2/Delphi

740 views Asked by At

Hope I can get the point across here..

I have 2 Workflows:

  1. (Test-Success) (c).NET WebAPI REST endpoint > (d)WebRequest to > (e)another .NET WebAPI REST endpoint > (f)Delphi.NET library
  2. (Fails): (a)ASP.NET 5 Core REST endpoint > (b)HttpClient.SendAsync > (c)WebAPI REST endpoint> (d)WebRequest to > (e)another .NET WebAPI REST endpoint > (f)Delphi.NET library (blocks when calling the underlying Delphi method)

(2) is target state. From (c) onwards, it exactly the same as 1).

  • In (2) Fiddler shows that correct url/headers are passed at (c) to the .WebAPI REST endpoint - but it never returns until the ASP.NET Core call (b) times out.

Things tried (not exhaustive)

  • added ConfigureAwait(false) all way down the stack
  • rewriting .NET Framework code to use HttpClient
  • adding Async in the code calling Delphi.NET
  • Removing async in HttpClient calls
  • Adding EnableCors in Startup for the final endpoint
  • Tried increasing and decreasing timeouts (on client and server)
  • ConsoleApp to simplify the process

Standard Code to use HttpClient

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:xxxxx");
        client.DefaultRequestHeaders.Add("header1", "xxx");


        var uri = $"{client.BaseAddress}v1/CoreEndpoint";
        string jsonContent = JsonConvert.SerializeObject("{body}");

        var m = new HttpRequestMessage { RequestUri = new Uri(uri), Method = HttpMethod.Post };
        m.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

        HttpResponseMessage response = await client.SendAsync(m);
1

There are 1 answers

1
Markus Meyer On

Maybe there's a missing slash:

    client.BaseAddress = new Uri("http://localhost:xxxxx");
    client.DefaultRequestHeaders.Add("header1", "xxx");


    var uri = $"{client.BaseAddress}v1/CoreEndpoint";

=> http://localhost:xxxxxv1/CoreEndpoint

Does it work if you change it to:

    var uri = $"{client.BaseAddress}/v1/CoreEndpoint";