I have a console application which is running as scheduler, and same consumes a Web API and have two sections as mentioned below
- Posting files to API - where i am sending some files to API to get some information from the files.
- Getting Response from the API- where i am receiving the response from the API which i have already send to the API previously.
So process will flow as follows. When scheduler runs initially, I will be sending some files (example A,B and C) to the API. And API will time some time to process the files.
So next time when scheduler runs it will post some more files D,E,F etc and will try to get the response of A,B,C
skeleton of code as follows
static void Main(string[] args)
{
//"starting Posting files to API";
PostDatas(ds);
//"Getting Response from the API";
GetResponseXML(ds);
}
public static void PostDatas(DataSet ds)
{
var client = new HttpClient();
foreach (DataRow dr in ds.Tables[0].Rows)
{
//post the files
var response = client.PostAsync(URL, ReqClass, bsonFormatter).Result;
}
}
public static void GetResponseXML(DataSet ds)
{
var clientResponse = new HttpClient();
foreach (DataRow dr in ds.Tables[1].Rows)
{
//get the response.
var response = clientResponse.PostAsync(URL,ReqClass, bsonFormatter).Result;
}
}
}
Here all the process are in synchronous way which is causing too much time.
I want to make the posting process and getting response in asynchronous way. posting multiple files together as well as getting response for multiple files parallel
How can i achieve this? use the threading concept or use the async and await concept or TPL(Task parallel library). What changes i should perform in above code to make it work asynchronously.
Please help with samples.