2

I have a VB.NET function as below:

Public Shared Async Function GetIdDoc() As Task(Of String)
    Dim result As String = ""
    'Dim Uri As String = "http://localhost:53917/api/Documenti/GetNextIdDocumenti"
    Dim Uri As String = apiUri & ApiEndPoints.GetNextIdDocumenti

    Using client = New HttpClient()
        Using response = Await client.GetAsync(Uri)
            If response.IsSuccessStatusCode Then
                Dim DocumentiIDJsonString = Await response.Content.ReadAsStringAsync()
                result = DocumentiIDJsonString.ToString()

            End If
        End Using
    End Using
    Return result
End Function

I'm trying to return the Document ID from the DB but I'm getting

System.Threading.Tasks.Task`1[System.String]

Where actually it should return "2". Please help me on this: what am I doing wrong with this function?

Update

here is the function called:

 txtIDDoc_Detail.Text = ApiData.GetIdDoc().ToString()

But inside the textbox I'm getting the above text. thanks.

3
  • Please edit your question to also show the code where the function is called. Commented Oct 27, 2016 at 18:34
  • Please check my updated question. Thanks. Commented Oct 27, 2016 at 18:37
  • You should probably rename GetIdDoc to GetIdDocAsync to follow the generally recommended pattern for async methods. Commented Oct 27, 2016 at 19:56

2 Answers 2

9

I'm from C# but should work the same. In newer .Net Versions (>= 4.5) async/await is implemented. So if a method is marked as async and returns a Task (which should always be the case), you need to await it. This implicate that you have to mark your Method as async too. So your call should look like this:

txtIDDoc_Detail.Text = await ApiData.GetIdDoc();

The await waits till the long running Task is ready and returns it's inner value. All async Methods should return Task. If the Method is void it would be Task. Else it could be Task<int> or any other type. So await it and you can keep running ;)

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

2 Comments

Or txtIDDoc_Detail.Text = ApiData.GetIdDoc.Result if you want to run it synchronously (for whatever reason).
@BradleyUffner - ApiData.GetIdDoc.Result can cause a deadlock.
0

@Sebi gives a great explanation of how to properly use async and await in this case, but I'm going to expand on exactly why you are getting the result that you see.

 txtIDDoc_Detail.Text = ApiData.GetIdDoc().ToString()

Is returning

System.Threading.Tasks.Task`1[System.String]

Because you are calling .ToString on the instance of the Task Task(Of String), not the actual result. Types that don't override .ToString inherit the behavior of Object, which just returns the name of the type as a string.

You probably want this (asynchronous call):

txtIDDoc_Detail.Text = await ApiData.GetIdDoc() 

Or this (synchronous call):

txtIDDoc_Detail.Text = ApiData.GetIdDoc().Result 

Either of these calls will actually result of the task after it has completed.

3 Comments

ApiData.GetIdDoc().Result can cause a deadlock, check this for more information: Don't Block on Async Code
It will cause a deadlock if the GetIdDoc(); Method returns a result in MainThread. Because the Method want to return it while Result try to access it.
Thanks for explaining my issue.

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.