ielcoro
9/25/2012 - 3:18 PM

Implementación del patrón APM en un WebService clásico usando Task

Implementación del patrón APM en un WebService clásico usando Task

<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service2
    Inherits System.Web.Services.WebService

    <WebMethod>
    Public Function BeginHelloWorld(ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult

        Dim tarea As Task(Of Boolean) = Task(Of Boolean).Factory.StartNew(Function() HelloWorld(), state)

        If Not callback Is Nothing Then
            tarea.ContinueWith(Sub(t) callback(tarea))
        End If

        Return tarea
    End Function

    Private Function HelloWorld() As Boolean
        Threading.Thread.Sleep(5000)

        Return True
    End Function

    <WebMethod>
    Public Function EndHelloWorld(ByVal result As IAsyncResult) As Boolean
        Return CType(result, Task(Of Boolean)).Result
    End Function

End Class