Ultron1891
Ultron1891

Reputation: 21

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

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();

Upvotes: 0

Views: 163

Answers (2)

djv
djv

Reputation: 15774

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.

Private Sub oneShot()
    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 = [Delegate].Combine(
                        clientHandler.ServerCertificateCustomValidationCallback,
                        Function(sender As HttpRequestMessage, certificate As X509Certificate2, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) True)
                End If
                Return message
            End Function
        End Sub).Build()

End Sub

Private Sub brokenUp()
    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 = [Delegate].Combine(clientHandler.ServerCertificateCustomValidationCallback, callback)
                      End If
                      Return message
                  End Function
    Dim hubConnection = New HubConnectionBuilder().WithUrl($"your host/chatHub", Sub(opts) opts.HttpMessageHandlerFactory = factory).Build()
End Sub

Upvotes: 1

Dave Doknjas
Dave Doknjas

Reputation: 6542

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.

Upvotes: 1

Related Questions