0

There is an answer here which has this code in C#:

public class GoogleBatchInterceptor : IHttpExecuteInterceptor
{
    public async Task InterceptAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        await Task.CompletedTask;
        if (request.Content is not MultipartContent multipartContent)
        {
            return;
        }
        
        foreach (var content in multipartContent)
        {
            content.Headers.Add("Content-ID", Guid.NewGuid().ToString());
        }
    }
}

I am trying to port this to VB.Net and I tried to begin with:

Imports Google.Apis.Http.IHttpExecuteInterceptor

Public Class GoogleBatchInterceptor :   IHttpExecuteInterceptor

End Class

It says:

Declaration expected.

How do I port this code over? It is so I can temporarily circumnavigate a Google Calendar API issue


Update

I have got this far now:

Imports System.Net.Http
Imports System.Threading
Imports Google.Apis.Http

Public Class GoogleBatchInterceptor
    Implements IHttpExecuteInterceptor

    Private Async Function IHttpExecuteInterceptor_InterceptAsync(request As HttpRequestMessage, cancellationToken As CancellationToken) As Task Implements IHttpExecuteInterceptor.InterceptAsync
        Await Task.CompletedTask
        'If request.Content Is Not MultipartContent multiplepartContent Then
        '    Return
        'End If

        'For Each (dim content)


    End Function
End Class
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164

1 Answers1

2

There is no direct equivalent for the pattern-matching used in that C# code. The closest VB equivalent would be this:

Imports System.Net.Http
Imports System.Threading
Imports Google.Apis.Http

Public Class GoogleBatchInterceptor
    Implements IHttpExecuteInterceptor

    Public Async Function InterceptAsync(request As HttpRequestMessage, cancellationToken As CancellationToken) As Task Implements IHttpExecuteInterceptor.InterceptAsync
        Await Task.CompletedTask

        Dim multipartContent = TryCast(request.Content, MultipartContent)

        If multipartContent Is Nothing Then
            Return
        End If

        For Each content In multipartContent
            content.Headers.Add("Content-ID", Guid.NewGuid().ToString())
        Next
    End Function

End Class
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • If this is for .NET Framework, I don't think you can make it async; at least in my testing, the VB compiler isn't OK with that even though it ought to be allowable (VS 2019 / .NET Framework 4.8). It might work with .NET 5.0. Regardless of whether it can be done, I'd still recommend against it, and just return `Task.CompletedTask` instead of the vacuous await. (I made the same comment on the C# answer and will submit a proposed edit there as soon as the queue isn't full.) – Craig Sep 27 '21 at 17:20