如何使用async和await发布webapi调用

时间:2017-07-11 21:34:33

标签: c# asp.net-web-api async-await

我正在尝试编写一个尝试使用async和await发布webapi调用的webapi,我的当前问题是,只要我将await client.PostAsync(url, content);挂起就挂起。

1.如何调试它挂起的原因?

2.有没有办法在没有异步和等待的情况下做到这一点?我想按顺序执行

    public static async Task<string> testWCF2(string xmlConfig)
    {
        string submitOut;

        using (var client = new System.Net.Http.HttpClient())
        {
            var url = "http://server:8100/api/SoftwareProductBuild";
            var content = new StringContent(xmlConfig, Encoding.UTF8, "application/xml");
            var response = await client.PostAsync(url, content);
            if (response.IsSuccessStatusCode)
            {
                var responseBody = await response.Content.ReadAsStringAsync();
                submitOut = responseBody;
            }
            else
            {
                submitOut = string.Format("Bad Response {0} \n", response.StatusCode.ToString());
                submitOut = submitOut + response;
            }
        }

        return submitOut;
    }

    public async Task<string> QlasrSubmit(List<XMLSiInfo> xmlConfigs)
    {
        string submitOut = "QLASR: ";

        foreach (XMLSiInfo xmlConfig in xmlConfigs)
        {
           submitOut = submitOut + "\n" + await testWCF2(xmlConfig.xml);
        }

        return submitOut;
    }


    public async Task<string> QlasrPostcommit(string si, string sp, string variant = null)
    {
        .....
        string submitStatus     = await QlasrSubmit(siInfo);
        .....
        return submitStatus;
    }

服务:

    public async Task<string> QlasrPostcommit(string si, string sp, string variant = null)
    {
        return await DPR.QlasrPostcommit(si, sp, variant);
    }

控制器:

[Route("api/DevPool/QlasrPostcommit")]
[HttpPost]
public ResponseObject QlasrPostcommit(string si, string sp, string variant = null)
{
    ResponseObject response = new ResponseObject();
    try
    {
        response.status = 200;
        response.data = DPS.QlasrPostcommit(si, sp, variant);
        return response;
    }
    catch (Exception e)
    {
        response.status = 200;
        response.data = null;
        response.message = e.Message;
        return response;
    }
}

2 个答案:

答案 0 :(得分:2)

您应该使用我async all the way中提到的your previous question

[Route("api/DevPool/QlasrPostcommit")]
[HttpPost]
public async Task<ResponseObject> QlasrPostcommit(string si, string sp, string variant = null)
{
  ResponseObject response = new ResponseObject();
  try
  {
    response.status = 200;
    response.data = await DPS.QlasrPostcommit(si, sp, variant);
    return response;
  }
  catch (Exception e)
  {
    response.status = 200;
    response.data = null;
    response.message = e.Message;
    return response;
  }
}

在这种特殊情况下,您正在遇到deadlock because you're blocking on asynchronous code

答案 1 :(得分:0)

我解决了它并且它完美地工作,没有deallock和等待结果!!

您已修复服务:

public string QlasrPostcommit(string si, string sp, string variant = null)
    {
 Task<string > task = Task.Run<string >(async () => await 
      DPR.QlasrPostcommit(si, sp, variant));
      task.Result;    
    }

通用答案:

public TypeToReturn MyAsyncMethod(myParams...)
        {
     Task<TypeToReturn> task = Task.Run<TypeToReturn>(async () => await 
          MyAsyncMethod(myParams...));
          task.Result;    
        }