Reputation: 1957
I am using C# code to send an Xml to an api end point and capture the response The way I do it is as follows I have folder A with 100 xmls, folder B with 100 Xmls and folder C with 100 xmls
I loop through each of the folders and in each loop iteration I create a task. Lets call this as folder task
The folder task loop through all the xml in each folder and captures the response. This is accomplished in sendrequestandcaptureresponse() method
The issue I am facing is the loop end before all the xmls are processed. sendrequestandcaptureresponse() method is triggered for all the 300 XMLS ( which is in folder A, B and C ) but I get the response for only 150 (approximately) XMLS
Task.WaitAll(tasklist) exits before waiting for all XML responses
Please find the code below
Code to Iterate through the folder
foreach(Folder in Folders){
Task t=Task.Factory.StartNew(
async () =>
{
Httpmode.RunRegressionInHTTPMode(folderPath);
}
}).Unwrap();
tasklist.Add(t);
}
Task.WaitAll(tasklist.ToArray());
Code which sends request and captures response
public void RunRegressionInHTTPMode(folderPath){
try
{
string directoryPath = CurrServiceSetting.GetDirectoryPath();
var filePaths = CreateBlockingCollection(folderPath+"\\Input\\");
int taskCount = Int32.Parse(CurrServiceSetting.GetThreadCount());
var tasklist = new List<Task>();
for (int i = 0; i < taskCount; i++)
{
var t=Task.Factory.StartNew(
async () =>
{
string fileName;
while (!filePaths.IsCompleted)
{
if (!filePaths.TryTake(out fileName))
{
continue;
}
try{
SendRequestAndCaptureResponse(fileName,CurrServiceSetting,CurrSQASettings,testreportlocationpath);
}
catch(Exception r)
{
Console.WriteLine("#####SOME Exception in SendRequestAndCaptureResponse "+r.Message.ToString());
}
}
}).Unwrap();
tasklist.Add(t);
}
Task.WaitAll(tasklist.ToArray());
}
catch(Exception e)
{
}
}
Could anyone please point me what I am missing here. I made the task as async task and added Unwrap method at the end of the task but still not able to wait till all the task are finished.
Any help would be great.
Thanks in advance
Adding the SendRequestAndCaptureResponse code below
public void SendRequestAndCaptureResponse(string fileName,ServiceSettings curServiceSettings,SQASettings CurrSQASettings,string testreportlocationpath){
XmlDocument inputxmldoc = new XmlDocument ( );
Stream requestStream=null;
byte[] bytes;
DateTime requestsenttime=DateTime.Now;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create ( url );
string responseStr = "";
bytes = Encoding.ASCII.GetBytes ( str4 );
try{
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
Credentials credentials = new Credentials();
request.Credentials = new NetworkCredential (CurrSQASettings.GetUserName(), CurrSQASettings.GetPassword());
request.Method = "POST";
request.Timeout = Int32.Parse(curServiceSettings.GetTimeOut());
//OVERRIDING TIME
requestsenttime=DateTime.Now;
requestStream = request.GetRequestStream ( );
requestStream.Write ( bytes, 0, bytes.Length );
requestStream.Close ( );
}
catch(Exception e){
return;
}
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse ( );
if (response.StatusCode == HttpStatusCode.OK)
{
allgood = true;
Stream responseStream = response.GetResponseStream ( );
responseStr = new StreamReader ( responseStream ).ReadToEnd ( );
XmlDocument xml = new XmlDocument ( );
xml.LoadXml ( responseStr );
xml.Save(resultantactualxmlpath);
response.Close();
responseStream.Close();
}
}
catch(Exception e){
return;
}
}
Upvotes: 0
Views: 3353
Reputation: 3990
You don't await the inner Task:
foreach(Folder in Folders){
Task t=Task.Factory.StartNew(
async () =>
{
await Httpmode.RunRegressionInHTTPMode(folderPath); // <--- await here
}
}).Unwrap();
tasklist.Add(t);
}
Task.WaitAll(tasklist.ToArray());
Therefor, your iterations doesn't wait for the inner Tasks to end - in each iteration you just create a Task and moves on.
A more elegant way to create the Tasks would be:
var tasks = Folders.Select(p=> Httpmode.RunRegressionInHTTPMode(p)).ToArray();
Task.WaitAll(tasks);
(typo insensitive)
Upvotes: 2