gpt4 book ai didi

c# HttpClient PostAsync with async and await 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:58 26 4
gpt4 key购买 nike

尝试使用 HttpClient 从 WebApi 1 发布到 WebApi 2 时,我遇到了死锁情况PostAsync并使用 asyncawait .

下面是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 的任何响应。我已经阅读了几篇关于死锁情况的在线文章。但是,我无法解决我案件中的僵局。我在这里做错了什么?我在使用 async 吗?和 await合适吗?

最佳答案

为了建立我上面的评论,修改你的代码,这样你就不会阻塞 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

关于c# HttpClient PostAsync with async and await 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47964127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com