Home > Enterprise >  Trying to port this code from C# to VB.NET (derives from IHttpExecuteInterceptor)
Trying to port this code from C# to VB.NET (derives from IHttpExecuteInterceptor)

Time:09-27

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

CodePudding user response:

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
  • Related