Jeff Short
Jeff Short

Reputation: 285

How to post a webapi call using async and await

I am trying write a webapi which tries to post a webapi call using async and await,my current issue is as soon as I call await client.PostAsync(url, content); it hangs.

1.How to debug why it is hanging?

2.Is there a way to do it without async and await?I want to do it sequentially

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

Service:

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

Controller:

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

Upvotes: 2

Views: 5058

Answers (2)

user1012506
user1012506

Reputation: 2118

I solved it and it works perfectly, without deallock and with waiting result!!

You have fix the Service:

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

Generic answer:

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

Upvotes: 0

Stephen Cleary
Stephen Cleary

Reputation: 456507

You should use async all the way, as I mentioned in 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;
  }
}

In this particular case, you're running into a deadlock because you're blocking on asynchronous code.

Upvotes: 3

Related Questions