1

I guess i am doing something terrible wrong here...

Dim s As String = GetResponse("", New KeyValuePair(Of String, String))

  Public Async Function GetResponse(ByVal url As String, ByVal params As KeyValuePair(Of String, String)) As System.Threading.Tasks.Task(Of String)
        Dim values = New List(Of KeyValuePair(Of String, String))
        Dim httpClient = New HttpClient(New HttpClientHandler())
        Dim response As HttpResponseMessage = Await httpClient.PostAsync(url, New FormUrlEncodedContent(values))
        response.EnsureSuccessStatusCode()
        Return Await response.Content.ReadAsStringAsync()
    End Function

any ideas please?

2 Answers 2

4

Your function returs not a string. it returns a Task(Of String), like it says in the error message and as you can see in your function declaration. You see, it is not working as you think.

Solution should be: Try calling GetResponse("", New KeyValuePair(Of String, String)).Result, that will do what you want. The Result is the type like the type parameter of your Task.

Sign up to request clarification or add additional context in comments.

Comments

2

You should Await tasks to "unwrap" their result, as such:

Dim s As String = Await GetResponse("", New KeyValuePair(Of String, String))

Yes, this does mean that the calling method does need to be Async, so it's callers must use Await, etc. This is perfectly natural; Async does "infect" the code base like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.