Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an Async Sub in VB.NET?

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

Repsoitory 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

Repository Implementation:

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

I'm trying to call the InsertFailedLoginAttemptAsync repository method in my manager. It is an async method but I am unable to await the method. How can I make this method awaitable?

I believe it has something to do with the interface and not making it an async method like in C# but I'm unable to do this.

like image 345
Andrew Avatar asked Jan 29 '23 17:01

Andrew


2 Answers

Subs should not be async. Event handlers are the only exception to that rule. You await Task which can only be returned from a Function. If the intention is to make that interface async then all the members need to be functions that return a Task or its derivative.

Async is something that bubbles all the way through when used. That said the ILoginManager along with the ILoginRepository should be refactored (if possible) to follow the proper syntax.

Reference: Async/Await - Best Practices in Asynchronous Programming

like image 119
Nkosi Avatar answered Feb 03 '23 17:02

Nkosi


Fixed via Nkosi's reply:

Interface:

Public Interface ILoginRepository
    Function IsUserAuthenticatedAsync(ByVal cID As String, ByVal password As String, ByVal IsExternalUser As Boolean) As Task(Of Boolean)
    Function InsertFailedLoginAttemptAsync(ByVal failedLoginAttempt As FailedLogin) As Task
    Function InsertFailedLoginAttemptIntoLoginMasterAsync(ByVal failedLoginAttempt As FailedLogin) As Task

End Interface

Manager method:

 Public Async Function InsertFailedLoginAttempt(failedLoginAttempt As FailedLogin) As Task Implements ILoginManager.InsertFailedLoginAttempt
        'Example of the S in Solid (Single Repsonsibilty)
        Await _iLoginRepository.InsertFailedLoginAttemptAsync(failedLoginAttempt)
        Await _iLoginRepository.InsertFailedLoginAttemptIntoLoginMasterAsync(failedLoginAttempt)
    End Function
like image 20
Andrew Avatar answered Feb 03 '23 16:02

Andrew