locked
Calling a WebMethod from Javascript issue. RRS feed

  • Question

  • User-106241125 posted

    I am calling a server-side webmethod from inside a javascript loop on the client via a Pagemethods call. The loop is exactly 300 items and the calls to the server are all sent quickly and pile up on the server. See below:

    Javascript
    ==================================
    for (i = 0; i < selectedValues.length; i++) {

         PageMethods.WM_PostInvoice(strParamList, OnSuccessCallback, OnFailureCallback);
    }          

    Webmethod on Server
    ==================================
    Public Shared Function WM_PostInvoice(ByVal strParamList As String) As String

            Try
           
                'Threading.Thread.Sleep(900) 'OK - 900 milliseconds(0.9 seconds) X 300 requests = 270 seconds = 4.5mins
                'Threading.Thread.Sleep(1000) 'OK  - 1000 milliseconds(1 second) X 300 requests = 300 seconds = 5mins
               
         'Threading.Thread.Sleep(1100) 'Fails  - 1100 milliseconds(1.1 seconds) X 330 requests = 270 seconds = 5.5mins
                'Threading.Thread.Sleep(1200) 'Fails  - Etc.
                'Threading.Thread.Sleep(1500) 'Fails  - Etc.

                '    strReturn &= "|0|Success!"

            Catch ex As Exception

                strReturn &= "|-1|Error: " & ex.Message

            End Try


            Return strReturn

        End Function

    The problem I'm having is, whenever the sum of the call totals over 5 minutes, all calls AFTER 5 minutes fail. Any suggestions to make calls that have been qued but not processed within 5 minutes not fail? I have tried changing lots of setting without any success. See below.

    On the page
    =============================
    <%@ Page Title="" ...... AsyncTimeout="800" %>
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" AsyncPostBackTimeout="600" >

    On the code behind page
    =============================
    Server.ScriptTimeout = 3600

    Web.config
    =============================
    <httpRuntime executionTimeout="600" />
    <sessionState timeout="90" />

    Sunday, August 26, 2018 3:17 PM

All replies

  • User753101303 posted

    Hi,

    And the error you have is ? It happens under "normal" circumstances or when slowing down down intentionally your web site ?

    Ususally you would post a list rather than individual items so that you can achieve the same result wiht just few calls and avoid the latency you get wiht each and every call. Make sure also they are all needed. For example if this is following changes you could post maybe only updated items rather than to post them all.

    Monday, August 27, 2018 8:33 PM
  • User-106241125 posted

    The problem I'm having is, whenever the sum of the call totals over 5 minutes, all calls AFTER 5 minutes fail. Any suggestions to make calls that have been qued but not processed within 5 minutes not fail? I have tried changing lots of setting without any success. See below.

    Monday, August 27, 2018 11:40 PM
  • User283571144 posted

    Hi mitchpetel,

    According to your description, I have created a test demo on my side, it works well.

    It could run well after 5 minutes.

    Could you please post the details error message?

    More details about my test demo, you could refer to below codes:

    ASPX:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                for (i = 0; i < 300; i++) {
                    var strParamList="111";
                    PageMethods.WM_PostInvoice(strParamList);
                }
            })
            //$(function () {
            //    for (i = 0; i < 800; i++) {
            //        var strParamList = "111";
            //        PageMethods.WM_PostInvoice(strParamList);
            //    }
            //})
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" AsyncPostBackTimeout="600"></asp:ScriptManager>
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

    Imports System.Web.Services
    Imports System.Threading
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
                      Server.ScriptTimeout = 3600
    End Sub
    
                <System.Web.Services.WebMethod>
                Public Shared Function WM_PostInvoice(ByVal strParamList As String) As String
                      Dim strReturn As String=" "
                      Try
                            Thread.Sleep(900)
                            Thread.Sleep(1000)
                            Thread.Sleep(1100)
                            Thread.Sleep(1200)
                            Thread.Sleep(1500)
    
                            strReturn &= "|0|Success!"
                      Catch ex As Exception
                            strReturn &= "|-1|Error: " & ex.Message
    
                      End Try
                      Return strReturn
                End Function
    

    Result:

    Best Regards,

    Brando

    Tuesday, August 28, 2018 9:04 AM
  • User-106241125 posted

    Thanks for the input Brando

    Try modifying your code to catch the successful and failed calls. In my case all calls executed 5 mins after I click the button, are caught by the OnFailureCallback function. See code below:

    PageMethods.WM_PostInvoice(strParamList, OnSuccessCallback, OnFailureCallback);
    
    -----------------------------------------------------------
    function OnSuccessCallback(results) { //alert(results); } function OnFailureCallback(error) { var stackTrace = error.get_stackTrace(); var message = error.get_message(); var statusCode = error.get_statusCode(); var exceptionType = error.get_exceptionType(); var timedout = error.get_timedOut(); var n = Date.now(); //var varRepNum = Number(txtInvCntr.GetText()) + 1; // Display the error. var RsltElem = document.getElementById("Results"); RsltElem.innerHTML = "Stack Trace: " + stackTrace + "<br/>" + "Service Error: " + message + "<br/>" + "Status Code: " + statusCode + "<br/>" + "Exception Type: " + exceptionType + "<br/>" + "Timedout: " + timedout + "<br/>" + "Rep Number: " + varRepNum; }

    Tuesday, August 28, 2018 6:59 PM
  • User753101303 posted

    It could help to let us know which error details are captured.

    Regardless of what happens, IMO you should redesign your API (AFAIK a browser can send around 10 requests before blocking on the next ones). Even if not directly related to your help it should help and you'll liekly have also a much better performance.

    Wednesday, August 29, 2018 7:45 AM