Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone, I'm trying to learn rabbitmq with the tutoria(RabbitMQ - RabbitMQ tutorial - "Hello World!"[^]). When I want to extract the receieve code block to a function as NewMethod, and use the NewMethod in Main method. I cannot receive the message in the queue. Anyone can help, thanks a lot.
here are my Receive.cs code.
C#
<pre> public static void Main()
    {
        Console.WriteLine("message from NewMethod:" + NewMethod());
        Console.ReadLine();
    }

    static string NewMethod()
    {
        string message = string.Empty;
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "hello",
                                durable: false,
                                exclusive: false,
                                autoDelete: false,
                                arguments: null);
            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                message = Encoding.UTF8.GetString(body);
                Console.WriteLine("[x] Received {0}", message);

            };
            channel.BasicConsume(queue: "hello",
                                noAck: true,
                                consumer: consumer);
        }
        return message;
    }



What I have tried:

I have tried function without return value. but also failed
Posted
Updated 14-Mar-17 22:15pm

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900