0

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();
  • welcome to stackoverflow. what have you [***tried yourself***](https://idownvotedbecau.se/noattempt/) so far? what problems did you encounter? what have you researched? please **edit** your question to include more information. i recommend [taking the tour](https://stackoverflow.com/tour), as well as reading [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [what's on topic](https://stackoverflow.com/help/on-topic). – Franz Gleichmann Jan 31 '22 at 13:18
  • 1
    The only thing I see there that looks like it might be trouble is the subscription of a function to what looks like an event handler. – Craig Jan 31 '22 at 14:26
  • Show us your attempt at the VB.NET code first. – Dave Doknjas Jan 31 '22 at 14:35
  • 1
    @Craig: The pattern matching variable - clientHandler (not available in VB) and the confusing nature of the triply-nested lambda might also be trouble. – Dave Doknjas Jan 31 '22 at 16:32
  • @DaveDoknjas I'd agree that they're potentially more troublesome for a hand translation, but I'm a little surprised that they would be an issue for an automated translator. (I started out writing "I don't see anything that should be an issue for an automated translator" and revised when I saw the subscription of the function.) – Craig Jan 31 '22 at 18:11

2 Answers2

1

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
djv
  • 15,168
  • 7
  • 48
  • 72
1

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.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • I thought Event at first too but I'm just not seeing it. This code doesn't compile. – djv Jan 31 '22 at 18:16
  • 1
    @djv I think it's making a multicast delegate, though I'm not sure how that would behave if there are multiple subscribers returning different results---maybe last one called wins? The `+=` in C# is syntactic sugar for operations that can be performed in VB using `Delegate.Combine`. – Craig Jan 31 '22 at 21:51
  • @Craig seems right, changing my answer according to that. Is it customary to have a callback property with no mechanism to determine whether it is a multicast delegate? I guess it's OK as long as `Delegate.Combine` is *always* used and works in any case. – djv Jan 31 '22 at 22:05
  • I think it's basic .NET facilities for delegates, and I doubt that the library expects a multicast delegate. For that matter, I believe even the C# code will only really create a multicast delegate for the case where there is already a callback provided. I think it's more straightforward to just assign a callback, but the multicast approach does seem like it would be the direct translation. – Craig Feb 01 '22 at 13:45