RabbitMQ示例无法在WPF中运行

时间:2017-08-19 08:45:54

标签: c# wpf rabbitmq

我正在尝试在WPF应用程序下使用rabbitMQ。我已经关注了rabbitmq站点上的示例。

发件人是一个

的控制台应用程序
   static void Main(string[] args)
    {
        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);

                    string message = "Hello World!";
                    var body = Encoding.UTF8.GetBytes(message);

                    channel.BasicPublish(exchange: "",
                        routingKey: "hello",
                        basicProperties: null,
                        body: body);
                    Console.WriteLine(" [x] Sent {0}", message);
                }
            }

    }

MainWindowViewModel执行

 public class MainWindowViewModel :ViewModelBase
{
    protected override Task InitializeAsync()
    {
        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.Registered += Consumer_Registered;
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] Received {0}", message);
            };
            channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer);

        }

        return base.InitializeAsync();
    }

    private void Consumer_Registered(object sender, ConsumerEventArgs e)
    {
        int t = 0;
    }
}

此代码不适用于wpf。同样放在控制台应用程序中

我注意到Consumer_Registered被解雇了......任何人都有类似的问题?到目前为止,我已经看到频道创建是好的

由于

0 个答案:

没有答案