在管理器类中使用 VB.NET 异步等待

问题描述 投票:0回答:1

我正在开发一个应用程序,当我发送对 API 的调用时,我遇到一个问题,它不会执行后一行,每当我在 API 端点上添加调试器并花费几秒钟时,它对我来说工作得很好。问题在于我使用 VB.NET 编写的管理器类发送的异步调用。 GetBaseApiUrlAsync 问题出在这个方法上,我担心我的结构使用异步等待不太好。以下是我经理的课程

Public Class HttpWebRequestManager

    Private Property mCurrMsg As MfgIntChargeItProSetupMessage = Nothing
    Private bearerToken As String
    Private baseApiUrl As String
    Private oID As String
    Public Property mUiContext As UIContext

    Public Sub New(mCurrentMsg As MfgIntChargeItProSetupMessage, mCurrContext As UIContext)
        mCurrMsg = mCurrentMsg
        mUiContext = mCurrContext
        Initialize()
    End Sub

    Private Async Sub Initialize()
        Dim locationSettings = (From d In mCurrMsg.Locations Select d Where d.LocationId = mUiContext.Context.DefaultLocationId).FirstOrDefault()
        If locationSettings IsNot Nothing Then
            oID = locationSettings.OID
            bearerToken = locationSettings.AuthToken
        Else
            Charter.UI.MessageBoxes.ShowInformation("Please setup OID and AuthToken for emerge pay to proceed.")
        End If

        baseApiUrl = Await GetBaseApiUrlAsync()
        Dim tt = String.Empty

    End Sub

    Private Async Function GetBaseApiUrlAsync() As Task(Of String)
        Dim baseApiUrl = mUiContext.Cache.GetEmergePayBaseAddress()

        If String.IsNullOrEmpty(baseApiUrl) Then
            Dim posCallerService As POSCallerService = New POSCallerService(mUiContext.Context.ProductKey, mUiContext.Context.UserName)
            Dim config = Await posCallerService.GetPOSConfigurationsAsync()
            baseApiUrl = config.EmergePayBaseAddress
            mUiContext.Cache.SetEmergePayBaseAddress(baseApiUrl)
        End If

        Return baseApiUrl
    End Function

End Class
.net vb.net async-await
1个回答
0
投票

您需要使您的Initialize方法成为一个返回任务的函数,然后在调用方法中等待它。无法等待异步子。

Private Async Function Initialize() as Task


Public Async Sub New(mCurrentMsg As MfgIntChargeItProSetupMessage, mCurrContext As UIContext)
    mCurrMsg = mCurrentMsg
    mUiContext = mCurrContext
    Await Initialize()
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.