How to call late bound extension method from VB.NET (Framework)

42 Views Asked by At

I have successfully used MQTTnet in C#. One customer insists that I use VB.NET, so I converted my C# to VB.NET. Now I get an error, 'Error BC36908 Late-bound extension methods are not supported' on the line Await managedMqttClientPublisher.EnqueueAsync(message).

  Private managedMqttClientPublisher As IManagedMqttClient 
  Private IsConnected As Boolean = False

  Private IsStartProcessing As Boolean = False
  Public Async Function PublisherStart() As Task
    Try
      If Not IsStartProcessing Then
        IsStartProcessing = True
        If Not IsConnected Then
          Dim mqttFactory = New MqttFactory()

          Dim tlsOptions = New MqttClientTlsOptions With {
.UseTls = False,
.IgnoreCertificateChainErrors = True,
.IgnoreCertificateRevocationErrors = True,
.AllowUntrustedCertificates = True
}

          Dim options = New MqttClientOptions With {
.ClientId = "ClientPublisher",
.ProtocolVersion = MqttProtocolVersion.V311,
              .ChannelOptions = New MqttClientTcpOptions With {
  .Server = "localhost",
  .Port = MQTTMessagePort,
  .TlsOptions = tlsOptions
}
}

          If options.ChannelOptions Is Nothing Then
            Throw New InvalidOperationException()
          End If

          options.Credentials = New MqttClientCredentials("username", Encoding.UTF8.GetBytes("password"))

          options.CleanSession = True
          options.KeepAlivePeriod = TimeSpan.FromSeconds(5)
          managedMqttClientPublisher = mqttFactory.CreateManagedMqttClient()
          'Me.managedMqttClientPublisher.ConnectedAsync += AddressOf OnPublisherConnected
          AddHandler Me.managedMqttClientPublisher.ConnectedAsync, AddressOf OnPublisherConnected
          AddHandler Me.managedMqttClientPublisher.DisconnectedAsync, AddressOf OnPublisherDisconnected
          AddHandler Me.managedMqttClientPublisher.ApplicationMessageReceivedAsync, AddressOf Me.Subscriber.HandleReceivedApplicationMessage

          Await managedMqttClientPublisher.StartAsync(New ManagedMqttClientOptions With {
.ClientOptions = options
})
        End If
        IsConnected = True
      End If
    Catch ex As Exception
      AccutrolLibrary.Logging.LogException(ex)
    End Try
    IsStartProcessing = False
  End Function

  Public Async Function Publish(ByVal Topic As String, ByVal PayloadString As String) As Task(Of String)
    Dim Result = ""
    Dim sb As StringBuilder = New StringBuilder()

    Try
      'sb.Append("{\"Topic\": \"");
      'sb.Append(Topic + "\",");
      'sb.Append("\"Message\": \"");
      'sb.Append(Message + "\",}");
      'var payload = Encoding.UTF8.GetBytes(sb.ToString());
      Dim payload = Encoding.UTF8.GetBytes(PayloadString)
      Dim message = New MqttApplicationMessageBuilder().WithTopic(Topic.Trim()).WithPayload(payload).WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce).WithRetainFlag().Build()

      If managedMqttClientPublisher IsNot Nothing Then
        Await managedMqttClientPublisher.EnqueueAsync(message)
      End If
    Catch ex As Exception
      'MessageBox.Show(ex.Message, "Error Occurs", MessageBoxButtons.OK, MessageBoxIcon.Error);
      Result = "Error: " & ex.Message
    End Try
    Return Result
  End Function

I have searched the Internet to discover how to correct this error. The only entries I have found so far discuss modifying the library that contains the late-bound extension method. I am not comfortable modifying the MQTTnet library.

I have tried replacing the call with Await IManagedMqttClient.EnqueueAsync(message), but that did not correct the error.

Is there a way to structure the call to the late-bound extension method so that it does not produce an error?

0

There are 0 best solutions below