c#HttpClient PostAsync with async并等待无法正常工作

问题描述 投票:2回答:1

当我尝试使用HttpClient PostAsync并使用asyncawait从WebApi 1发布到WebApi 2时,我遇到了僵局。

以下是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;
    }

}

和WebApi2:

[System.Web.Http.HttpPost]
public HttpResponseMessage ReceiveXml(HttpRequestMessage request)
{

    var xmlDoc = new XmlDocument();
    xmlDoc.Load(request.Content.ReadAsStreamAsync().Result);

    xmlDoc.Save(@"C:\xmlfiles\xml2.xml");

    XmlSerializer deserializer = new XmlSerializer(typeof(OrderInfoModel));
    TextReader reader = new StreamReader(@"C:\xmlfiles\xml2.xml");
    object obj = deserializer.Deserialize(reader);
    OrderInfoModel orderdetails = (OrderInfoModel)obj;
    reader.Close();

    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);
}

我没有从WebAPI 2回复WebAPI 1回复。我在网上看了几篇关于死锁情况的文章。但是,我无法解决我的情况下的僵局。我在这做错了什么?我正确使用asyncawait吗?

c# asynchronous asp.net-web-api async-await http-post
1个回答
0
投票

要在上面建立我的评论,修改你的代码,这样你就不会阻止async操作。此外,_orderServices.GetOrderDetails(oid);听起来像一个命中数据库的方法,因此应该是await _orderServices.GetOrderDetailsAsync(oid);,其中你使用任何async api可用于您的数据库访问。

[HttpGet()]
public async Task<HttpResponseMessage> Get([FromUri]int oid) {
    var orderdetails = _orderServices.GetOrderDetails(oid);

    var xml = new XmlMediaTypeFormatter();
    xml.UseXmlSerializer = true;
    string orderdetailsser = Serialize(xml, orderdetails);

    var result = await 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;
    }

}

[HttpPost()]
public async Task<HttpResponseMessage> ReceiveXml(HttpRequestMessage request) {

    var xmlDoc = new XmlDocument();
    xmlDoc.Load(await request.Content.ReadAsStreamAsync());

    xmlDoc.Save(@"C:\xmlfiles\xml2.xml");

    XmlSerializer deserializer = new XmlSerializer(typeof(OrderInfoModel));
    TextReader reader = new StreamReader(@"C:\xmlfiles\xml2.xml");
    object obj = deserializer.Deserialize(reader);
    OrderInfoModel orderdetails = (OrderInfoModel)obj;
    reader.Close();

    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);
}

资源

Don't Block on Async Code

Avoid Async Void

© www.soinside.com 2019 - 2024. All rights reserved.