public void Run() { var queue = RabbitMQHelper.GetOrderQueueName(); var factory = new ConnectionFactory() { HostName = RabbitMQHelper.GetServerHostname() }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: queue, durable: true, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { //Our body is the json of our orderRequest object. var body = ea.Body; string json = System.Text.Encoding.UTF8.GetString(ea.Body, 0, ea.Body.Length); var orderRequest = JsonConvert.DeserializeObject <OrderRequest>(json); _orderService.AddOrder(orderRequest); Console.WriteLine(" [x] Received {0}", json); }; channel.BasicConsume(queue: queue, autoAck: true, consumer: consumer); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } }
public void SendOrder(OrderRequest orderRequest) { var factory = new ConnectionFactory() { HostName = RabbitMQHelper.GetServerHostname() }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: RabbitMQHelper.GetOrderQueueName(), durable: true, exclusive: false, autoDelete: false, arguments: null); var body = JsonConvert.SerializeObject(orderRequest); channel.BasicPublish(exchange: "", routingKey: RabbitMQHelper.GetOrderQueueName(), basicProperties: null, body: System.Text.Encoding.UTF8.GetBytes(body), mandatory: true); } }