0

Below is a code snippet in which I get data from a third-party API with async methods.

jQuery code:

function retrieveSummary() {
    $.ajax({
        type: "POST",
        url: 'api/GetLeads',
        dataType: "json",
        async: true,
        error: function (xhr, textStatus, thrownError) {
        },
        success: function (data) {
        },
        failure: function (data) {
        }
    });
}

C# Code with an async method in which the above JSON stuck, wrote comments.

[HttpPost]
[ActionName("GetLeads")]
public List<Leads> GetSummary()
{
    List<Leads> list = new List<Leads>();
    
    var result = RunAsync(publicKey, secretKey).Result;
    var data = result.GetData();
    // Process data and return but it stucks on below async method RunAsync in await line.
    .
    .
}

static async Task<Response> RunAsync(string publicKey, string secretKey)
{
    Client client = new Client(publicKey, secretKey);
    
    Request request = new Request()
    {
        Resource = Message.Resource
    };
    
    request.Filter(Message.Limit, limit)
        .Filter("FromTS", String.Format("{0:s}", fromDate))
        .Filter("ToTS", String.Format("{0:s}", toDate))
        .Filter("ShowContactAlt", "true")
        .Filter("ShowSubject", "true")
        .Filter("MessageStatus", 3)
        .Filter(Message.Offset, offSet);
    
    // Ajax call stucks here.
    return await client.GetAsync(request);
}
k-s
  • 2,192
  • 11
  • 39
  • 73
  • 2
    Could you try adding `.ConfigureAwait(false)` after `client.GetAsync(request)`, and see if it makes any difference? – Theodor Zoulias Sep 15 '22 at 08:06
  • 2
    Yes, it seems working fine after ConfigureAwait, can you please explain to me the reason behind this? Thank you so much. @TheodorZoulias – k-s Sep 15 '22 at 08:43
  • 1
    Check out this question: [An async/await example that causes a deadlock](https://stackoverflow.com/questions/15021304/an-async-await-example-that-causes-a-deadlock). If you think that it answers sufficiently your question, you can close this one as a duplicate (by clicking the "Close" button under the tags). – Theodor Zoulias Sep 15 '22 at 09:29

0 Answers0