public async Task StartAsync() { var tcs = new TaskCompletionSource <object>(); var connectionInfo = await RabbitMqConnctionFactory.CreateAsync(); var connection = connectionInfo.Item1; var channel = connectionInfo.Item2; this.currentTask = Task.Run(async() => { using (connection) using (channel) { channel.ExchangeDeclare(exchange: RabbitMqConstants.ExchangeName, type: ExchangeType.Fanout); var message = "integration test message"; var body = Encoding.UTF8.GetBytes(message); tcs.SetResult(null); while (!this.cts.IsCancellationRequested) { channel.BasicPublish(exchange: RabbitMqConstants.ExchangeName, routingKey: "", basicProperties: null, body: body); await Task.Delay(publisherDelay); } } }); }
public async Task StartAsync() { if (connection != null) { throw new InvalidOperationException("already started"); } var connectionInfo = await RabbitMqConnctionFactory.CreateAsync(); connection = connectionInfo.Item1; channel = connectionInfo.Item2; channel.ExchangeDeclare(exchange: RabbitMqConstants.ExchangeName, type: ExchangeType.Fanout); var queueName = channel.QueueDeclare().QueueName; channel.QueueBind(queue: queueName, exchange: RabbitMqConstants.ExchangeName, routingKey: ""); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var message = Encoding.UTF8.GetString(body); Console.WriteLine(" [x] {0}", message); if (consumeDelay > 0) { Thread.Sleep(consumeDelay); } }; channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer); }