Home > Net >  Need some help converting a c# code snippet to VB.Net
Need some help converting a c# code snippet to VB.Net

Time:02-01

I have code snippet written in C# that I need to convert to VB.NET. I've been struggling with this for hours and hours. Also tried tools like Telerik Converter and Vistual Studio plugin but I always end up with some compile error...sigh... I would really appreciate if someone could help me with this... Thanks in advance... This is the snippet:

hubConnection = new HubConnectionBuilder()
     .WithUrl($"your host/chatHub", (opts) =>
     {
         opts.HttpMessageHandlerFactory = (message) =>
         {
             if (message is HttpClientHandler clientHandler)
                 // bypass SSL certificate```

                 clientHandler.ServerCertificateCustomValidationCallback  =
                     (sender, certificate, chain, sslPolicyErrors) => { return true; };
             return message;
         };
     }).Build();

CodePudding user response:

The C# code is not as clear as one would hope, but the VB.NET equivalent should be the following (assuming that the " =" is wiring up an event handler):

hubConnection = (New HubConnectionBuilder()).WithUrl($"your host/chatHub", Sub(opts)
    opts.HttpMessageHandlerFactory = Function(message)
        If TypeOf message Is HttpClientHandler Then
            Dim clientHandler As HttpClientHandler = CType(message, HttpClientHandler)

            ' bypass SSL certificate```

            AddHandler clientHandler.ServerCertificateCustomValidationCallback, Function(sender, certificate, chain, sslPolicyErrors)
                Return True
            End Function
        End If
        Return message
    End Function
End Sub).Build()

Personally, I would break up the triply-nested lambdas.

CodePudding user response:

The callback assignment looks like it could be replaced with an event handler, but from here, it appears you can just assign a lambda to the callback property.

Dim hubConnection = New HubConnectionBuilder().WithUrl(
    $"your host/chatHub",
    Sub(opts)
        opts.HttpMessageHandlerFactory =
        Function(message)
            Dim clientHandler = TryCast(message, HttpClientHandler)
            If clientHandler IsNot Nothing Then clientHandler.ServerCertificateCustomValidationCallback =
                Function(sender, certificate, chain, sslPolicyErrors)
                    Return True
                End Function
            Return message
        End Function
    End Sub).Build()

Breaking up the lambdas

Dim callback = Function(sender As HttpRequestMessage, certificate As X509Certificate2, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) True
Dim factory = Function(message As HttpMessageHandler)
                    Dim clientHandler = TryCast(message, HttpClientHandler)
                    If clientHandler IsNot Nothing Then clientHandler.ServerCertificateCustomValidationCallback = callback
                    Return clientHandler
                End Function
Dim hubConnection = New HubConnectionBuilder().WithUrl($"your host/chatHub", Sub(opts) opts.HttpMessageHandlerFactory = factory).Build()
  •  Tags:  
  • Related