gpt4 book ai didi

vb.net - 如何在 VB.NET 中编写异步子程序?

转载 作者:行者123 更新时间:2023-12-04 23:38:43 24 4
gpt4 key购买 nike

Public Class LoginManager
Implements ILoginManager
Private ReadOnly _iLoginRepository As ILoginRepository
Public Sub New()
_iLoginRepository = New LoginRepository()
End Sub

Public Async Sub InsertFailedLoginAttempt(failedLoginAttempt As FailedLogin) Implements ILoginManager.InsertFailedLoginAttempt
'Example of the S in Solid (Single Repsonsibilty)
'Need to call these method async. But await errors
_iLoginRepository.InsertFailedLoginAttemptAsync(failedLoginAttempt)
_iLoginRepository.InsertFailedLoginAttemptIntoLoginMasterAsync(failedLoginAttempt)
End Sub
End Class

存储库接口(interface):
Public Interface ILoginRepository
Function IsUserAuthenticatedAsync(ByVal cID As String, ByVal password As String, ByVal IsExternalUser As Boolean) As Task(Of Boolean)
Sub InsertFailedLoginAttemptAsync(ByVal failedLoginAttempt As FailedLogin)
Sub InsertFailedLoginAttemptIntoLoginMasterAsync(ByVal failedLoginAttempt As FailedLogin)

End Interface

存储库实现:
Public Class LoginRepository
Implements ILoginRepository
Public ReadOnly _applicationDBContext As New ApplicationDBContext()

Public Async Sub InsertFailedLoginAttemptAsync(failedLoginAttempt As FailedLogin) Implements ILoginRepository.InsertFailedLoginAttemptAsync
Using _applicationDBContext
_applicationDBContext.RepFailedLogins.Add(failedLoginAttempt)
Await _applicationDBContext.SaveChangesAsync()
End Using
End Sub

Public Async Sub InsertFailedLoginAttemptIntoLoginMasterAsync(failedLoginAttempt As FailedLogin) Implements ILoginRepository.InsertFailedLoginAttemptIntoLoginMasterAsync
Using _applicationDBContext
_applicationDBContext.RepFailedLoginMasters.Add(failedLoginAttempt)
Await _applicationDBContext.SaveChangesAsync()
End Using
End Sub

''' <summary>
''' Determine whether a user is authenticated, be it an internal or external user
''' I have condensed two methods into one
''' </summary>
''' <param name="cID"></param>
''' <param name="password"></param>
''' <param name="IsExternalUser"></param>
''' <returns></returns>
Public Async Function IsUserAuthenticatedAsync(cID As String, password As String, IsExternalUser As Boolean) As Task(Of Boolean) Implements ILoginRepository.IsUserAuthenticatedAsync
If (IsExternalUser And String.IsNullOrEmpty(password)) Then
Throw New ArgumentNullException("External user requires password")
End If

Dim user As Chaser
Dim toRet As Boolean

Using _applicationDBContext
'Two ways to use LINQ
'First is LINQ Lambda sybntax(little harder to read)
user = Await _applicationDBContext.Chasers.Where(Function(x) x.CID = cID).FirstOrDefaultAsync()

'Second is LINQ Query syntax(looks more like SQL just more verbose
'user = From x In _applicationDBContext.Chasers
' Where x.CID = cID
' Select x
End Using

If IsNothing(user) Then
toRet = False
ElseIf Not IsExternalUser And Not IsNothing(user) Then
toRet = True
ElseIf IsExternalUser And user.Hash_Password = password Then
toRet = True
End If

Return toRet
End Function
End Class

我正在尝试调用 InsertFailedLoginAttemptAsync我的经理中的存储库方法。这是一个异步方法,但我无法等待该方法。我怎样才能让这个方法可以等待?

我相信它与接口(interface)有关,而不是像 C# 那样使它成为异步方法,但我无法做到这一点。

最佳答案

Sub s 不应该是异步的。事件处理程序是该规则的唯一异常(exception)。你等着Task只能从 Function 返回.如果打算使该接口(interface)异步,则所有成员都需要是返回 Task 的函数。或其衍生物。

异步是在使用时一直冒泡的东西。那说ILoginManager连同 ILoginRepository应该重构(如果可能的话)以遵循正确的语法。

引用:Async/Await - Best Practices in Asynchronous Programming

关于vb.net - 如何在 VB.NET 中编写异步子程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45870469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com