.net - Async Await Handler Deadlock -
i'm stuck in async deadlock , can't figure out correct syntax fix it. i've looked @ several different solutions, can't seem quite figure out causing problem.
i using parse backend , trying use handler write table. handler looks like:
public class visitorsignuphandler : ihttphandler { public void processrequest(httpcontext context) { //get user's name , email address var userfullname = context.request.querystring["name"].urldecode(); var useremailaddress = context.request.querystring["email"].urldecode(); //save user's information var tasktoken = usersignup.saveusersignup(userfullname, useremailaddress); tasktoken.wait(); .... } public bool isreusable { { return false; } } } then calling middle tier:
public static class usersignup { public static async task saveusersignup(string fullname, string emailaddress) { //initialize parse client application id , windows key parseclient.initialize(appid, key); //create object var userobject = new parseobject("usersignup") { {"userfullname", fullname}, {"useremailaddress", emailaddress} }; //commit object await userobject.saveasync(); } } although seems getting stuck @ wait(). under impression wait() wait task complete, return normal operations. not correct?
you're running common deadlock problem describe on blog , in recent msdn article.
in short, await default resume async method inside of captured "context", , on asp.net, 1 thread allowed "context" @ time. when call wait, blocking thread inside context, , await cannot enter context when ready resume async method. thread in context blocked @ wait (waiting async method complete), , async method blocked waiting context free... deadlock.
to fix this, should go "async way". in case, use httptaskasynchandler instead of ihttphandler:
public class visitorsignuphandler : httptaskasynchandler { public override async task processrequestasync(httpcontext context) { //get user's name , email address var userfullname = context.request.querystring["name"].urldecode(); var useremailaddress = context.request.querystring["email"].urldecode(); //save user's information var tasktoken = usersignup.saveusersignup(userfullname, useremailaddress); await tasktoken; .... } }
Comments
Post a Comment