3

Didn't found a single thing about how to use RabbitMQ in VB

It is possible to make changes to some extend, to make it look like C#

Dim factory = New ConnectionFactory() : With factory
  .HostName = "localhost"
End With

Using connection = factory.CreateConnection()
  Using channel = connection.CreateModel()

  ...
  ...

  End Using
End Using

And I already have checked that there are no problems with sending messages or declaring queues, but what about usage of consumer?

In C# Consumer looks like:

var consumer = new EventingBasicConsumer(channel);

        consumer.Received += (model, ea) =>
        {
            var body = ea.Body;

            ...
            ...

            channel.BasicAck(ea.DeliveryTag, false, false);
        };

But how this supposed to look in VB? Already tried this kind of stuff:

Dim consumer As EventingBasicConsumer = New EventingBasicConsumer(channel)
AddHandler consumer.Received, AddressOf ConsumerReceived

Private Sub ConsumerReceived(ByVal sender As Object, ByVal e As EventArgs)
  Dim Test = ""
End Sub

Consumer is created without a problem, but there are no reaction on event

Is there some kind of work around, manual how to use RabbitMQ with VB or maybe I am simply missing something?

4
  • I upvoted your question and I read it. But my only hint were to use an online converter. Commented Nov 21, 2017 at 14:27
  • 1
    People on So have a strict diet, please do not add salt directly into your question. They may have an intolerence. You can instead ask how to improve your question. I see Nothing wrong in your question, but i'm not an expert. Only a low rep passing by. Commented Nov 21, 2017 at 14:33
  • 1
    Sadly, online converter can't understand what to do with converter.Received. I simply don't believe that no one tried to use RabbitMQ on VB before :( There must be something Commented Nov 21, 2017 at 14:37
  • The right thing to do in this circumstance is to adapt and start using c#, which is a far less cumbersome programming language. :) Commented Nov 21, 2017 at 20:46

1 Answer 1

4

The question really seems to be how to assign an anonymous function as an event handler in VB.NET. It's not the same question as why the event handler isn't getting called.

But I think what you're looking for may be this:

AddHandler consumer.Received, Sub(consumer as IBasicConsumer, ea as BasicDeliverEventArgs)
    Dim body = ea.Body
    '...  
    channel.BasicAck(ea.DeliveryTag, false, false)
End Sub

Or you could do as you are and put the event handler in a separate method. (I lean toward that just because it's easier to read and follow.)

In your VB.NET method you're casting the event sender and args as object and EventArgs. The arguments can be cast to those types since everything is an object and BasicDeliverEventArgs inherits from EventArgs. But in this case you'd want to cast more specifically so that you can access the properties you need.

Private Sub ConsumerReceived(ByVal sender As IBasicConsumer, ByVal e As BasicDeliverEventArgs)
  Dim Test = ""
End Sub

The easiest thing (if it's an option) is just to switch to C#. That makes code samples easier to work with. I held on to VB.NET for a while. No idea why - it didn't take long to adjust to C# and I quickly came to prefer it.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thanks for your response and for example. Didn't even considered this way. Didn't worked out on first attempt, but Console.ReadKey made a magic. I also think that C# is way better, but for my situation, sadly, not an option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.