I have a ServiceInvoker which calls a Web API (POST endpoint)
public async Task<HttpResponseMessage> InvokeService(string url, string action)
{
string requestUri = @"api/MyController/" + action;
HttpResponseMessage response;
HttpClientHandler handler = new HttpClientHandler()
{
UseDefaultCredentials = true
};
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
response =await client.PostAsJsonAsync(requestUri, myObj);
}
return response;
}
When I debug, after the PostAsJsonAsync executes, the control goes to main(). Also without debugging I am not able to hit the service. It works when I do this
response =client.PostAsJsonAsync(requestUri, myObj).Result;
but its synchronous.
This method is called from main()
void main()
{
Start()
}
public void Start()
{
Process();
}
public async Task Process()
{
HttpResponseMessage servResponse;
servResponse=await InvokeService("http://myServiceUrl","myAction");
}
My requirement is to make huge number of calls to Service from different applications. I want to make the call async to improve performance.