postman is not returning async call

I cannot return List 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;
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x