从Javascript并行使用多个.NET Web服务

时间:2011-06-29 00:01:36

标签: javascript vb.net web-services asmx

我有两个从javascript消耗的ASMX Web服务。第一个服务执行长操作并更新数据库表的进度。第二个服务轮询同一个数据库表,以便通过进度条向最终用户报告进度。

我的问题是,漫长的过程似乎阻止了轮询服务。当我记录javascript的活动时,它似乎正在请求正确的长服务,然后开始异步请求一次轮询服务(注意:长进程也是异步)。两种请求类型都使用不应停止浏览器的setIntervalsetTimeout。然而,当我查看javascript的活动时,轮询请求的响应都没有返回,直到长进程完成。因此,似乎漫长的过程会阻止轮询请求,直到完成为止。

这是细节:

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);

服务代码(VB.NET - 注意:代码已更改为通用)

   <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.ClientConnectionReadOnly
        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

1 个答案:

答案 0 :(得分:0)

将'OneWay = true'添加到CreateBulkOrder Webmethod,否则它将等待响应。

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