locked
How to set up async when using form page with multiple calls to web api 2 functions? RRS feed

  • Question

  • User1581761955 posted

    This project is a VS2015 project targeting .net 4.5 it calls a web api 2 service.

    So I have a form with about 50 calls to a function set up like this where each row calls the getWidgetTableCells() function.
    The problem with 50 sequential web service calls is that it takes 8 seconds or so to complete all the calls. So I am tyring
    to call these asynchronously.

                <tr>
                  <td style="text-align:center">1</td>
                  <td colspan="2">Widget result 1 </td>
                    <%=getWidgetTableCells("1db") %>
                  </tr>
                <tr>
                  <td style="text-align:center">2</td>
                  <td colspan="2">Widget result 2 </td>
                    <%=getWidgetTableCells("2ch") %>
                  </tr>
                  .
                  .
                  .
                <tr>
                  <td style="text-align:center">n</td>
                  <td colspan="2">Widget result n </td>
                    <%=getWidgetTableCells("nxx") %>
                  </tr>
                <tr>



                
    The getWidgetTableCells() function calls a web service (web api 2 as follows). It uses the client.GetAsync
    but that doesn't seem to be helping. Is there something else I need to do?

    Public Function getWidgetTableCells(ByVal inData As String) As String
    
            Dim returnCells As String = ""
            'Start Building HTML
                        Dim client As New HttpClient()
                        Dim serviceUrl As String
    
                        serviceUrl ="http://apps.widgets.com/widgetdata/api/AppServices/DataVerbose/"                   
    
                        'CALLING WEB SERVICE
                        serviceUrl = serviceUrl + "1" + "/" + inData.ToLower
                        Dim response As HttpResponseMessage = client.GetAsync(serviceUrl).Result
                        Dim WidgetModelInstance As WidgetModelVerbose 'Create a local instance of the Widget Model that is found in the Web API web service
    
                        If response.IsSuccessStatusCode Then
                            ' If the service call is successful we then populate the Widget Model on our side.
                            WidgetModelInstance = response.Content.ReadAsAsync(Of WidgetModelVerbose)().Result  
    
                                'We do lots of other things depending if we need to adjust or format the results here    
    
                                'Create Widgets cell
                                returnCells = "<td style='text-align:center'>" & WidgetModelInstance.DataNeeded & "</td>"
    
                                Return returnCells
                        Else
                        'Handle it
                        End If
    End Function



    Tuesday, October 31, 2017 3:32 PM

All replies

  • User283571144 posted

    Hi ManOfManyHats,

    So I have a form with about 50 calls to a function set up like this where each row calls the getWidgetTableCells() function.
    The problem with 50 sequential web service calls is that it takes 8 seconds or so to complete all the calls. So I am tyring
    to call these asynchronously.

    According to your description and codes, I found your page will send 50 request to the web api to get the results.

    Firstly, I should tell you how the html page works, the HTML page is parsed sequentially from beginning to end. As resources are encountered such as stylesheets, images or scripts, the browser fires off parallel requests for those resources.

    So your page will send the 50 requests from beginning to end. e.g: page loaded to "Widget result 1", it will send a request, page loaded to "Widget result 2", it will send a request...

    I suggest you could consider changing your design.

    You could send one request to the web api.

    In this request you could send a json list which contains all your need search parameter.

    In the web api method, you could using Parallel.ForEach method to call the web service to get results.

    In the html page, you could using jquery datatable to filter all the results to each table cell.

     

     

    Wednesday, November 1, 2017 2:58 AM
  • User-474980206 posted
    Webforms has rather limited async support. See

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/performance-and-caching/using-asynchronous-methods-in-aspnet-45#CreatingAsynchGizmos

    As the docs show you must make all the async calls before render. The practical solution is in page load to start a request for each webapi call. The webapi caller should save its results in a property. Then in the webpage, instead of making a call, you just access the results.
    Wednesday, November 1, 2017 11:51 PM