Skip to content

How to get channel from Received event? #1227

@rabbitLove520

Description

@rabbitLove520
class Program
{
    private static IModel channelForEventing;

    static void Main(string[] args)
    {
        ReceiveMessagesWithEvents();
    }

    private static void ReceiveMessagesWithEvents()
    {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        .... 
        IConnection connection = connectionFactory.CreateConnection();
        channelForEventing = connection.CreateModel();
        EventingBasicConsumer eventingBasicConsumer = new EventingBasicConsumer(channelForEventing);
        eventingBasicConsumer.Received += EventingBasicConsumer_Received;
        channelForEventing.BasicConsume("my.first.queue", false, eventingBasicConsumer);
    }

    private static void EventingBasicConsumer_Received(object sender, BasicDeliverEventArgs e)
    {
        var channel = sender as IModel; // invalid channel?
        channel.BasicAck(e.DeliveryTag, false); // ack failed.
    }
}

It seems I have channelForEventing, channel is invalid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions