开发者

Consuming Multiple .NET Web Services from Javascript in Parallel

开发者 https://www.devze.com 2023-03-16 14:16 出处:网络
I have two ASMX web services consumed from javascript. The first service performs a long operation and updates a database table on its progress. The second service polls that same database table in or

I have two ASMX web services consumed from javascript. The first service performs a long operation and updates a database table on its progress. The second service polls that same database table in order to report the progress to the end user via a progress bar.

My problem is that the long process seems to be blocking the polling service. When I log the activity of the javascript, it seems to be requesting the long service correctly, and then starts to request the polling service once a second asynchronously (note: the long process is asynch as well). Both request types either use setInterval or setTimeout which shouldn't halt the browser. Yet when I look at the activity of the javascript, none of the responses from the polling requests return until the long process completes. So it seems the long process is blocking the polling requests until it's done.

Here's the nitty gritty:

JavaScript:

var percentComplete = 0;
setTimeout(function ()
{
    MyWebService.CreateBulkOrder(serverVariable, function (result, eventArgs)
    {
        percentComplete = 100;
        completeOperation(result);
    });
}, 0);
var intID = setInterval(function ()
{
    if (percentComplete < 100)
    {
      MyWebService.GetStatus(serverVariable, callback);
    }
    else
    {
        clearInterval(intID);
    }
}, 1000);

Service Code (VB.NET - Note: code is changed to make it generic)

   <System.Web.Script.Services.ScriptService()>
<System.Web.Services.WebService(Namespace:="http://mydns.com/webservices")>
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<ToolboxItem(False)>

Public Class MyWebServices
    Inherits System.Web.Services.WebService

<WebMethod(EnableSession:=True)>
Public Function GetStatus(serverVariable As Integer) As Object
    Dim currentPage As Integer = 0
    Dim totalPages As Integer = Math.Ceiling(CType(If(Session("Number of Records"), Double) / CType(ConstantsCommon.TOTAL_PER_PAGE, Double))
    Using clientDB As ClientDataContext = FunctionsOrderMgmt.ClientCo开发者_如何学CnnectionReadOnly
        Dim repeatPageQuery = From repeatPage In clientDB.RepeatPages
                              Where repeatPage.KEY = serverVariable
                              Select repeatPage
        Dim repeatPageData = repeatPageQuery.SingleOrDefault()
        If repeatPageData Is Nothing Then
            currentPage = 0
        Else
            currentPage = If(repeatPageData.REPEAT_PAGE, 0)
        End If
        Return New With {.TotalPages = totalPages, .CurrentPage = currentPage}
    End Using
End Function

<WebMethod(EnableSession:=True)>
Public Function CreateBulkOrder(serverVariable As Integer) As Boolean
    If Not TestsPass Then
        Return False
    End If
    Try
        'Do stuff that takes a long time
    Catch ex As Exception
        Return False
    End Try
    Return True
End Function

End Class


Add 'OneWay = true' to the CreateBulkOrder Webmethod otherwise it will be waiting for a response.

http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapdocumentmethodattribute.oneway(v=vs.71).aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消