1

I'm trying to call data from third-party using web-service and this will take a long time to load data so I need to apply async/await to that method, I'm calling that method in .cs, I'm using like below, when I tried with 'Void' instead of 'Task' it will take a long time to load if I use Task event will not fire.

 [WebMethod]
    public List<SuggestedItemsInput> SaveSuggestedItems(string emailbody, string OrderHeaderID)
    {
        List<SuggestedItemsInput> result = null;

        //... passing header and neccesery things

        request.AddParameter("application/text; charset=utf-8", strFinalEmail, ParameterType.RequestBody);
        IRestResponse restResponse = client.Execute(request);

        result = JsonConvert.DeserializeObject<List<SuggestedItemsInput>>(restResponse.Content);


        //Save to database
        if (dtProducts.Rows.Count >= 1)
            dalPendingOrders.SaveSuggestedItems(dtProducts);
        LogMessage("Suggested Items for " + OrderHeaderID + " : " + products.Products.Count);

        return result;
    }

in .cs

  protected void btnClaim_Click(object sender, EventArgs e)
    {
        GetSuggestedItemsFromService();
    }

    private async void GetSuggestedItemsFromService()
    {
        List<SuggestedItemsInput> suggestedItems = await Task.Run(() => SaveAndGetSuggestedItemAsync());

        ViewState["sItems"] = suggestedItems;
        if (suggestedItems != null && suggestedItems.Count > 0)
        {
            GetSuggestedItems(Request["OrderRecordID"].ToString());
            lblInfo.Text = string.Empty;
        }
    }
    private List<SuggestedItemsInput> SaveAndGetSuggestedItemAsync()
    {
        OHDWebService OHDService = new OHDWebService();
        List<SuggestedItemsInput> suggestedItemsList = OHDService.SaveSuggestedItems(hdnPlainBody.Value, hfdOrderRecordID.Value);
        return suggestedItemsList;
    }

    private void GetSuggestedItems(string OrderRecordID)
    {
        dt = dalPendingOrders.GetSuggestedItems(OrderRecordID);
        if (dt.Rows.Count > 0)
        {
            grdSuggestedItems.DataSource = dt;
            grdSuggestedItems.DataBind();
        }
        else
        {
            lblInfo.Text = "No Data Found";
        }
    }
3
  • 1
    What do you mean by "in .cs"? It does make no sense at all. .cs is the extension for a C# code file. It can be found in any .Net source code project Commented Aug 26, 2019 at 7:36
  • Is you client code WinForms or WebForms? Makes a big difference. Commented Aug 26, 2019 at 12:18
  • im using webform Commented Aug 27, 2019 at 10:29

2 Answers 2

4

With ASP.NET Web Forms you have to register asynchronous code as a Page Task:

protected void btnClaim_Click(object sender, EventArgs e)
{
    RegisterAsyncTask(GetSuggestedItemsFromService);
}

private async Task GetSuggestedItemsFromService()
{
    List<SuggestedItemsInput> suggestedItems = await SaveAndGetSuggestedItemAsync();

    ViewState["sItems"] = suggestedItems;
    if (suggestedItems != null && suggestedItems.Count > 0)
    {
        GetSuggestedItems(Request["OrderRecordID"].ToString());
        lblInfo.Text = string.Empty;
    }
}

Check this article out:

Using Asynchronous Methods in ASP.NET 4.5

Don't use Task.Run in ASP.NET applications. It will use more resources and it will be slower.

Sign up to request clarification or add additional context in comments.

Comments

1

You will need to make your btnClaim_Click method async void, which will then await GetSuggestedItemsFromService, which should return a Task. Use async void only for event handlers. Your .cs Code should look like this:

protected async void btnClaim_Click(object sender, EventArgs e)
    {
        await GetSuggestedItemsFromService();
    }

    private async Task GetSuggestedItemsFromService()
    {
        List<SuggestedItemsInput> suggestedItems = await Task.Run(() => SaveAndGetSuggestedItemAsync());

        ViewState["sItems"] = suggestedItems;
        if (suggestedItems != null && suggestedItems.Count > 0)
        {
            GetSuggestedItems(Request["OrderRecordID"].ToString());
            lblInfo.Text = string.Empty;
        }
    }
// Rest of your code

Moreover, the convetion is to call a method only Async if it's actually async. Hence I recommend renaming SaveAndGetSuggestedItemAsync() to SaveAndGetSuggestedItem() (it has no async keyword).

3 Comments

Don't use Task.Run in ASP.NET applications. It will use more resources and it will be slower.
Lose the Task.Run(). Superfluous and even a little harmful. suggestedItems = await SaveAndGetSuggestedItemAsync() is enough.
This would be a valid answer for WinForms but I don't think it will even work for WebForms (aspx).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.