public static void TestSecureConnectionToRabbitMq(string rabbitmqhostname) { try { var cf = RabbitMqConnectionFactory.GetConnectionFactory(rabbitmqhostname); using (IConnection conn = cf.CreateConnection()) { using (IModel ch = conn.CreateModel()) { ch.QueueDeclare("rabbitmq-dotnet-test", false, false, false, null); ch.BasicPublish("", "rabbitmq-dotnet-test", null, Encoding.UTF8.GetBytes("Hello, World")); BasicGetResult result = ch.BasicGet("rabbitmq-dotnet-test", true); if (result == null) { Console.WriteLine("No message received."); } else { Console.WriteLine("Received:"); DebugUtil.DumpProperties(result, Console.Out, 0); } ch.QueueDelete("rabbitmq-dotnet-test"); } } } catch (BrokerUnreachableException bex) { Exception ex = bex; while (ex != null) { Console.WriteLine(ex.ToString()); Console.WriteLine("inner:"); ex = ex.InnerException; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public string GetMessage(string hostname, CancellationToken token, AutoResetEvent messageReceivedWaitHandle, AutoResetEvent channelCreatedWaitHandle) { var factory = RabbitMqConnectionFactory.GetConnectionFactory(hostname); using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.ExchangeDeclare(exchange: ExchangeName, type: ExchangeType, durable: true); var queueName = channel.QueueDeclare().QueueName; channel.QueueBind(queue: queueName, exchange: ExchangeName, routingKey: routingKey); channelCreatedWaitHandle.Set(); var consumer = new EventingBasicConsumer(channel); string myMessage = null; Console.WriteLine( $"Listening for messages on host:{hostname} for queue:{queueName}, exchange:{ExchangeName} with routing key:{routingKey}"); consumer.Received += (model, ea) => { var body = ea.Body; var message = Encoding.UTF8.GetString(body); var routingKey = ea.RoutingKey; myMessage = message; Console.WriteLine($"Received {routingKey}: {message}"); messageReceivedWaitHandle.Set(); }; var basicConsume = channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer); while (!token.IsCancellationRequested) { } return(myMessage); } }