Martin
Martin

Reputation: 35

vb.net Task(Of HttpResponseMessage) to HttpResponseMessage

I have a vb.net async httpResponse function like this:

Public Async Function getWebserviceResponse(ByVal sb As StringBuilder, ByVal baseUri As Uri, ByVal Method As String, ByVal User As String, ByVal Password As String) As Task(Of HttpResponseMessage)

    Dim client As HttpClient = New HttpClient()
    client.BaseAddress = baseUri

    Dim authHeader As AuthenticationHeaderValue = New AuthenticationHeaderValue(
        "Basic",
        Convert.ToBase64String(
            System.Text.ASCIIEncoding.ASCII.GetBytes(
                String.Format("{0}:{1}", User, Password))))

    client.DefaultRequestHeaders.Authorization = authHeader

    Dim content As New StringContent(sb.ToString, System.Text.Encoding.UTF8, "application/json")

    Dim resp As HttpResponseMessage
    Dim cancellationToken As CancellationToken

    If Method = "Post" Then
        resp = Await client.PostAsync(baseUri, content, cancellationToken)

    ElseIf Method = "Put" Then
        resp = Await client.PutAsync(baseUri, content, cancellationToken)

    End If

    Return resp

End Function

The problem is, that "resp" should return a "normal" HttpResponseMessage and not a Task(Of HttpResponseMessage)...

How can I get this? Thank you in advance for your help!

Best regards Martin

Upvotes: 1

Views: 5180

Answers (1)

David
David

Reputation: 218887

resp is an HttpResponseMessage. However, the method is Async. Note the method signature:

Public Async Function getWebserviceResponse(...) As Task(Of HttpResponseMessage)

So while the code is logically returning an HttpResponseMessage, the method technically returns a Task(Of HttpResponseMessage). This works exactly the same as the methods being invoked therein. Note here:

resp = Await client.PostAsync(baseUri, content, cancellationToken)

The PostAsync method returns a Task(Of HttpResponseMessage), but that line of code is putting an HttpResponseMessage into the resp variable. This is because of the use of the Await keyword.

To achieve the same behavior, any method which invokes your getWebserviceResponse() method should do the same:

someVariable = Await getWebServiceResponse(...)

Edit: Based on your comment below, you have this line of code:

Dim myStreamReader As New StreamReader(webserviceResponse.getWebserviceResponse(....).Content.ReadAsStream‌​Async().Result) 

Which I suspect should be changed to this to use the Await keyword:

Dim myStreamReader As New StreamReader(Await (Await webserviceResponse.getWebserviceResponse(....)).Content.ReadAsStream‌​Async())

Or, to break it up into something a little less confusing:

Dim responseMessage As HttpResponseMessage = Await webserviceResponse.getWebserviceResponse(....)
Dim responseStream As Stream = Await responseMessage.Content.ReadAsStreamAsync()
Dim myStreamReader As New StreamReader(responseStream)

Each individual async operation would need to be awaited.

Upvotes: 1

Related Questions