Search code examples
c#asp.netasynchronouspostman

Postman is not returning async call


I cannot return List<string> from webform async method via Postman. It just goes forever. Am I missing something very tricky here?

Pageload:

protected void Page_Load(object sender, EventArgs e)
{
    RegisterAsyncTask(new PageAsyncTask(testAsync));

    if (Request["Act"] == "testAsync")
    {
        Response.Write(JsonConvert.SerializeObject(testAsync()));
        Response.End();
    }
}

Method:

private async Task<List<string>> testAsync()
{
    List<string> list = await Task.Run(() => {
        var _return = new List<string>();
        _return.Add("1");
        _return.Add("2");
        return _return;
    });

    return list;
}

And I markup aspx page as Async="true"

This is the url in postman http://localhost:65008/services/carrental/callBackHandler.aspx?act=testAsync

Solution I have updated my code after E.J.Brennan advise. It worked. First I called the async method, then returned the List after method executes.

PageLoad

    protected void Page_Load(object sender, EventArgs e)
{
    RegisterAsyncTask(new PageAsyncTask(testAsync));

    if (Request["Act"] == "testAsync")
    {
        var a = testAsync();
    }

}

Method

    private async Task<List<string>> testAsync()
{
    List<string> list = await Task.Run(() => {
        var _return = new List<string>();
        _return.Add("1");
        _return.Add("2");
        return _return;
    });

    Response.Write(JsonConvert.SerializeObject(list));
    Response.End();

    return list;
}

Solution

  • You are calling the 'testAsync' function asynchronously, but immediately calling the response.end - you need to wait for the testAsync() function to finish, otherwise the response.end gets sent before the function has had a chance to run.