【问题标题】:Value of type 'System.Threading.Tasks.Task(Of String)' cannot be converted to 'String'. in VB.Net“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。在 VB.Net
【发布时间】:2021-07-19 04:45:26
【问题描述】:

我正在尝试使用 Asyn 调用,但在拆箱结果时遇到问题。这是我得到的错误信息:“System.Threading.Tasks.Task(Of String)”类型的值无法转换为“String”。 有人可以帮助实现这一目标。这是我使用的代码:

Private Sub Download_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'GET DOWNLOAD URL
    Dim download_url As String = Download_HttpClient(space_id, file_id)

    'ACCESS URL THROUGH GOOGLE CHROME
    Dim proc As New ProcessStartInfo()
    proc.FileName = "C:\Windows\System32\cmd.exe"
    proc.Arguments = "/c start chrome " + download_url
    Process.Start(proc)
End Sub

Public Async Function Download_HttpClient(ByVal spaceId As String, fileId As String) As Task(Of String)
    Dim cookieContainer As New CookieContainer
    Dim httpClientHandler As New HttpClientHandler
    httpClientHandler.AllowAutoRedirect = True
    httpClientHandler.UseCookies = True
    httpClientHandler.CookieContainer = cookieContainer
    Dim httpClient As New HttpClient(httpClientHandler)

    'GET LOGIN CREDENTIALS
    Dim login_url As String = host + "/common/Authentication.json"
    Dim login_parameter As String = "?id=" + user_id + "&pw=" + password
    Dim login_response As HttpResponseMessage = Await httpClient.GetAsync(login_url + login_parameter)
    Dim login_contents As String = Await login_response.Content.ReadAsStringAsync()
    'MessageBox.Show(login_contents)

    'GET DOWNLOAD ID
    Dim download_url As String = host + "/contentsmanagement/SingleContentsDownloadApi.json"
    Dim download_param As String = "?fileId=" + file_id + "&spaceId=" + space_id + "&type=alive"
    Dim download_response As HttpResponseMessage = Await httpClient.GetAsync(download_url + download_param)
    Dim download_contents As String = Await download_response.Content.ReadAsStringAsync()
    Dim downloadId As String = Nothing
    Dim map As Dictionary(Of String, String)
    map = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(download_contents)
    map.TryGetValue("id", downloadId)
    'MessageBox.Show(downloadId)
    'set DOWNLOAD URL
    Dim url As String = host + "/contentsmanagement/ContentsDownloadApi.json?id=" + downloadId
    Return url

End Function

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    当一个函数被声明为Async并返回一个Task(Of T)时,你需要在调用它的时候Await它才能得到Task产生的T对象,即:

    Dim download_url As String = Download_HttpClient(space_id, file_id)
    

    需要改成这样:

    Dim download_url As String = Await Download_HttpClient(space_id, file_id)
    

    您只能在声明为 Async 的方法中使用 Await。这意味着:

    Private Sub Download_Click(sender As Object, e As EventArgs) Handles Button1.Click
    

    需要改成这样:

    Private Async Sub Download_Click(sender As Object, e As EventArgs) Handles Button1.Click
    

    请注意,您应该只将Async Sub 用于事件处理程序,否则使用Async Function

    我通常会推荐阅读Async/Await,但这是一个有点高级的话题。作为一般规则,如果您需要调用现有的 Async 函数,例如HttpClient.GetAsync,那你也需要自己制作方法Async

    如果你有这样的Sub

    Private Sub SomeMethod()
    

    你这样称呼它:

    SomeMethod()
    

    那会变成这样:

    Private Async Function SomeMethodAsync() As Task
    

    还有这个:

    Await SomeMethodAsync()
    

    异常是一个事件处理程序,我已经在上面演示过。他们必须是Sub,而且你不要直接给他们打电话。

    同样,如果您有这样的Function

    Private Function SomeMethod() As T
    

    你这样称呼它:

    Dim result As T = SomeMethod()
    

    那会变成这样:

    Private Async Function SomeMethodAsync() As Task(Of T)
    

    还有这个:

    Dim result As T = Await SomeMethodAsync()
    

    如果你有一个不是Async 的方法并且你想在其中调用Async 方法,你必须使你的方法Async 并使用Await,然后创建任何调用它的方法@ 987654354@ 并在其中使用Await,并在链上向上使用,直到到达事件处理程序。这就是为什么通常建议,如果你打算使用Async,你几乎可以在任何地方使用它。

    如果让Async的所有东西都太繁琐,那么你可以同步调用一个异步方法并得到Task返回的结果,例如这个:

    Dim download_url As String = Download_HttpClient(space_id, file_id)
    

    变成这样:

    Dim download_url_task As Task(Of String) = Download_HttpClient(space_id, file_id)
    Dim download_url As String = download_url_task.Result
    

    或者只是这个:

    Dim download_url As String = Download_HttpClient(space_id, file_id).Result
    

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 1970-01-01
      • 2015-08-14
      • 2022-01-27
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      相关资源
      最近更新 更多