-3

I have Following API Call in c# and I want to make it Synchronous and it should wait till it comes backs from other modules where delete logic is performed.

  • 4
    I don't see any async code? What am I missing please? – Paul Suart Jul 13 '18 at 10:36
  • Why do you exactly want to make it sync? It's weird but review your return, since it's `async void` and it might be `async Task` and of course call your `CallExternalServiceAsync()` as `await CallExternalServiceAsync()` – Gonzo345 Jul 13 '18 at 10:39

2 Answers2

0

I belive the following code is your problem: CallExternalServiceAsync(..)

It should return a task, which you can await if you declare your function async. Otherwise, if you are just interested in waiting, you may call CallExternalServiceAsync(...).Wait(), assuming that it returns a Task.

0

As stated on comments, you might return a Task instead a void and perform the call of the CallExternalServiceAsync method with the await, assuming it's awaitable.

public async Task CallDeleteOrderApi()
{
    var responseMsg = await CallExternalServiceAsync(_apiPath, _validatePath, requestHeader);
}

Have a look at this question which explains how and why using async Task.

Basically, just specify async void return on method declaration when it's on an Event (for example frm_Load on WinForms). On custom methods, always declare it as async Task if you would just return a void (which is actually returning a Task) and async Task<int> if you, for example, want to return an int.

Gonzo345
  • 1,133
  • 3
  • 20
  • 42
  • Public HttpResponseMessage CallExternalServiceAsync(string apiUrl, string apiPath, FormUrlEncodedContent postContent) { var client = new HttpClient { BaseAddress = new Uri(apiUrl) }; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //Post implementation var response = client.PostAsync(apiPath, postContent).Result; return HttpResponseMessage; } – Vitthal Mokhare Jul 13 '18 at 10:50
  • I have above code for CallExternalServiceAsync on on postasync it still dosent wait – Vitthal Mokhare Jul 13 '18 at 10:56
  • @VitthalMokhare the problem is that you have to wait in your CallExternalServiceAsync clas for the result. – Darem Jul 13 '18 at 11:25