Search code examples
c#vb.netcode-conversion

VB .NET to C# Error: 'As' is not a member of 'Task(Of IFlurlResponse)'


I'm converting this C# line

return await _serviceUrl.WithOAuthBearerToken(bearerToken)
                                

.AppendPathSegment($"api/v1/accounts/{accountId}/identities/{identityId}")
                                .AllowAnyHttpStatus()
                                .PatchJsonAsync(model)
                                .As<Models.IdentityModel>();

To this line in VB .Net

Return Await _serviceUrl.WithOAuthBearerToken(bearerToken).AppendPathSegment($"api/v1/accounts/{accountId}/identities/{identityId}").AllowAnyHttpStatus().PatchJsonAsync(model).[As](Of Identity)()

But I got this error

'As' is not a member of 'Task(Of IFlurlResponse)'.

How do I fix it?

Thank in Advance


Solution

  • The straight translate would be:

    Return (Await _serviceUrl.WithOAuthBearerToken(bearerToken) _
      .AppendPathSegment($"api/v1/accounts/{accountId}/identities/{identityId}") _ 
      .AllowAnyHttpStatus() _ 
      .PatchJsonAsync(model) _
    ).As(Of Identity)()
    

    But I've just pulled out a project that uses Flurl and it doesn't seem to include As so I'm guessing that's some extension of something else..

    Long story short, when you have a method name that ends in Async and returns an awaitable, Await it inside parentheses before you call more methods (on the parentheses) if you want to chain

    Dim r = (Await(Await(Await x.AAsync()).BAsync()).CAsync()).D
    

    Simplistically, if a MethodAsync() returns a Task(Of X) then calling Await MethodAsync() gives you the X, otherwise you're calling methods on the Task, not the X


    Footnote:

    While it's necessary to use square brackets in [As] to define a method called As, it's not necessary to have them to call it