c# HttpClient PostAsync with async and await not working RRS feed

  • Question

  • User-42563190 posted


    I am running into a dead lock situation when trying to post to WebApi 2 from WebApi 1 using httpclient PostAsync using async and await.

    Below is my **WebAPI** 1

    public HttpResponseMessage Get([FromUri]int oid) 


          var orderdetails = _orderServices.GetOrderDetails(oid);

                var xml = new XmlMediaTypeFormatter();

                xml.UseXmlSerializer = true;

                string orderdetailsser = Serialize(xml, orderdetails);


                var result = PostXml(orderdetailsser);

                return Request.CreateResponse(HttpStatusCode.OK);


      public static async Task<HttpResponseMessage> PostXml(string str)


                using (var client = new HttpClient())


                    client.BaseAddress = new Uri("http://localhost:58285/");

                    var content = new StringContent(str);

                    var response = await client.PostAsync("api/default/ReceiveXml", content).ConfigureAwait(false);

                    return response;






            public HttpResponseMessage ReceiveXml(HttpRequestMessage request)


                var xmlDoc = new XmlDocument();



                XmlSerializer deserializer = new XmlSerializer(typeof(OrderInfoModel));

                TextReader reader = new StreamReader(@"C:\xmlfiles\xml2.xml");

                object obj = deserializer.Deserialize(reader);

                OrderInfoModel orderdetails = (OrderInfoModel)obj;


                var patient_id = _patientServices.ProcessPatient(orderdetails.Patient, orderdetails.OrderInfo);

                var orderid = _orderServices.ProcessOrder(orderdetails.Patient, orderdetails.OrderInfo, patient_id);

                if (orderdetails.OrderNotes != null && orderdetails.OrderNotes.Count() > 0)


                    var success = _orderServices.ProcessOrderNotes(orderid, orderdetails.OrderNotes);


                var prvid = _orderServices.ProcessOrderProvider(orderid, orderdetails.ReferringProvider);

                var shpngid = _orderServices.ProcessOrderShipping(orderid, orderdetails.ShippingInfo);

                var payerid = _orderServices.ProcessOrderPayer(orderid, orderdetails.Insurances);


                return Request.CreateResponse(HttpStatusCode.OK, orderid);


    I am not getting any response back to WebAPI 1 from WebAPI 2. I have gone through couple of articles online about deadlock situation. I am unable to resolve the dead lock in my case. What am I doing wrong here? Am I using the async and await properly?

    Sunday, December 24, 2017 9:34 PM

All replies

  • User-474980206 posted

    a few issues with your code. webapi 1 does an async post , and does nothing with the response. it would be clearer to:

         // fire and forget

    in webapi 2 you pass an unread result to a sync routine:


    it should be:

          xmlDoc.Load(await request.Content.ReadAsStreamAsync());

    and of course webapi 2 RecieveXml should be made async.

    note: I also wonder why you use so many sync methods, when they should be async (all the _orderService methods) 

    Monday, December 25, 2017 2:23 AM