C# post request stuck in WaitingForActivation even with timeout -
i trying simple post request using httpclient (which far can tell current correct way handle post requests.) however, code stalling on timeouttask.result, tasks stuck on status=waitingforactivation. looking @ server side of things, can tell post request being sent , data being returned, , have called wget manually , verified text data correctly returned server post request.
this happened when called task.result, tried add timeout task, must making mistake async because still hangs on timeouttask.result.
class httppostexample { private static readonly httpclient client = new httpclient(); public void processsentence() { string uri = "http://localhost:9000/?properties={%22annotators%22%3a%22tokenize%2cssplit%2cpos%22%2c%22outputformat%22%3a%22json%22}"; string result = postsync(uri, "the quick brown fox jumped on lazy dog"); debug.writeline(result); } public static async task<string> postasync(string uri, string postdata) { var content = new stringcontent(postdata); var response = await client.postasync(uri, content); var responsecontent = await response.content.readasstringasync(); return responsecontent; } public static async task<string> runtaskwithtimeout(task<string> task, double seconds) { if (await task.whenany(task, task.delay(timespan.fromseconds(seconds))) == task) { // task completed within timeout return task.result; } else { // timeout logic return "timed out"; } } public static string postsync(string uri, string postdata) { var task = postasync(uri, postdata); var timeouttask = runtaskwithtimeout(task, 2.0); return timeouttask.result; } }
since calling async
method synchronous method may want try , add configureawait(false)
on of await lines. idea use async
way or add configureawait(false)
avoid deadlocks.
check out blog post example: https://medium.com/bynder-tech/c-why-you-should-use-configureawait-false-in-your-library-code-d7837dce3d7f
Comments
Post a Comment