我的代码有什么问题(VB.NET异步等待和httplistener)

时间:2018-11-04 23:00:25

标签: vb.net async-await

我正在尝试运行httplistener,以便接收来自Web应用程序的回叫

然而,永无止境的context = Await httpListener.GetContextAsync 我可以远程登录到侦听器的端口,并且它会响应。

Class SSO
Public Sub AuthorizeAsync()
    Me.oAccessToken = Operations.AuthorizeAsync.Result

End Sub
'
Class Operations
    Shared Async Function AuthorizeAsync() As Task(Of SSO.AccessToken)
        Dim AccessToken As SSO.AccessToken
        Dim AuthorizationCodeTask As Task(Of String)
        Dim cts As New System.Threading.CancellationTokenSource
        AuthorizationCodeTask = Operations.RunCallbackListenAsync(cts.Token)
        Try
            OpenAutorizationUrl()
            Dim t As String
            t = Await AuthorizationCodeTask
            AccessToken = GetAccessToken(t)
        Catch ex As Exception
            logger.LogException(ex)
            Throw ex
        End Try
        Return AccessToken
    End Function
    Private Shared Async Function RunCallbackListenAsync(ByVal cts As System.Threading.CancellationToken) As Task(Of String)
        Dim retval As String = Nothing
        Dim blGo As Boolean = True
        Dim httpListener As New System.Net.HttpListener
        httpListener.Prefixes.Add(My.Settings.APICallBack)
        httpListener.AuthenticationSchemes = System.Net.AuthenticationSchemes.Anonymous
        httpListener.Start()
        If httpListener.IsListening Then
            Dim request As System.Net.HttpListenerRequest
            Dim context As System.Net.HttpListenerContext
            context = Await httpListener.GetContextAsync
            While Not cts.IsCancellationRequested And blGo
                context = Await httpListener.GetContextAsync
                Try
                    If cts.IsCancellationRequested Then
                        context.Response.Abort()
                    End If
                    request = context.Request
                Catch ex As Exception
                    logger.LogException(ex)
                End Try
                blGo = False
            End While
            If Not IsNothing(request) Then
                retval = ProcessRequest(request)
            Else
                Throw New ApplicationException("RunCallbackListenAsync: The request is Nothing")
            End If
            httpListener.Close()
        Else
            Throw New ApplicationException("RunCallbackListenAsync: listener is not listening")
        End If
        Return retval
    End Function
End class

如何导致context = Await httpListener.GetContextAsync处的封锁?

0 个答案:

没有答案