Why Async function returning System.Threading.Tasks.Task`1[System.String]?

barsan

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.

Sebi

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 ;)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

System Threading Tasks String To String Conversion Error

cannot convert from 'System.Threading.Tasks.Task<string>' to 'string'

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'string'

C# Async MVC method returns ContentResult System.Threading.Tasks.Task`1[System.Web.Mvc.JsonResult]

Cannot convert from 'System.Threading.Tasks.Task' to 'System.Collections.Generic.Dictionary<string,string>'

RenderPartialAsync returns System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult]

Expression of type 'System.Func`1[System.Threading.Tasks.Task]' cannot be used for return type 'System.Threading.Tasks.Task'

System.Threading.Tasks.Task Method Not Found

System.Threading.Tasks.Parallel

Async function returning Task(Of String) or String?

Method not found: 'System.Threading.Tasks.Task`1<!!0>

Value of type 'System.Threading.Tasks.Task(Of String)' cannot be converted to 'String'. in VB.Net

Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' in c#

Error CS0029: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'

Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR

Task conflict in System.Threading.Tasks and System.Runtime

Unable to cast object of type AsyncStateMachineBox System.Threading.Tasks.VoidTaskResult to type System.Threading.Tasks.Task

Getting a token from an external web API is returning system.threading.tasks

Strange execution jump when using async/await and System.Threading.Tasks.Parallel

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task>' to <System.Threading.Tasks.Task<bool>>'

The model item passed into the dictionary is of type 'System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`

Could not include namespace System.Threading.Tasks

Unable to load System.Threading.Tasks.Extensions

Cannot implicitly convert type 'System.Threading.Tasks.Task

Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task'

Moq Method called by System.Threading.Tasks.Task.Factory.StartNew

Issue with System.Threading.Tasks.Task.Factory.StartNew() in UWP?

Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task'

System.Threading.Tasks.Task executed in a loop strange behavior