c# - Failing to catch the response from Task.Factory.FromAsync -
i have object
public class objectlist { public string idreturn { get; set; } }
while running asynchronous requests task.factory.fromasync
, i'm failing catch response
foreach (var obj in objectlist) { var wreq = (httpwebrequest)webrequest.create(convert.tostring(geturl)); var taskresp = task.factory.fromasync<webresponse>(wreq.begingetresponse, wreq.endgetresponse, null); taskresp.continuewith(tsk => new streamreader(tsk.result.getresponsestream()).readtoend().trim()) .continuewith((task<string> trs) => { obj.idreturn = trs.result.tostring(); }); } return jsonhelper.jsonserializer(objectlist);
what missing?
you're starting asynchronous operation , moving on , serializing object before asynchronous operation has finished.
rather using continuewith
should await
value returned fromasync
results, way rest of code won't continue on until after request has finished. if want requests happen in parallel can construct of tasks , await task.whenall
run serialization code after requests have finished.
Comments
Post a Comment