如何提取rabbitmq接收方法 [英] How to extract method for rabbitmq receive method

查看:123
本文介绍了如何提取rabbitmq接收方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试用tutoria学习rabbitmq( RabbitMQ - RabbitMQ教程 - 你好世界! [ ^ ])。当我想将接收代码块提取到NewMethod函数时,并在Main方法中使用NewMethod。我无法在队列中收到消息。任何人都可以提供帮助,非常感谢。

这是我的Receive.cs代码。

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.

<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

推荐答案

请参阅 RabbitMQ - RabbitMQ社区 [ ^ ]。


这篇关于如何提取rabbitmq接收方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆