public void AddRabbitMqFactory_WithInvalidHostnameAndPort_ShouldProvideChannelFactory() { // Arrange var serviceCollection = new ServiceCollection(); var configuration = new DefaultRabbitMqConfiguration { Hostname = "does.not.exist", Port = 1234 }; // Act serviceCollection.AddRabbitMqFactory(configuration); // Assert var services = serviceCollection.BuildServiceProvider(); var connectionFactory = services.GetRequiredService <IConnectionFactory>() as ConnectionFactory; connectionFactory.Should().NotBeNull(); connectionFactory.HostName.Should().Be(configuration.Hostname); connectionFactory.Port.Should().Be(configuration.Port); var exc = Assert.Throws <BrokerUnreachableException>(() => services.GetRequiredService <IChannelFactory>()); exc.Should().NotBeNull(); exc.Message.Should().Be("None of the specified endpoints were reachable"); // no RabbitMQ is running in CI environment }
public void AddRabbitMqFactory_WithUriAndRequestedHeartbeatAndContinuationTimeout_ShouldProvideConfiguration() { // Arrange var serviceCollection = new ServiceCollection(); var configuration = new DefaultRabbitMqConfiguration { Uri = new Uri("amqp://localhost:5672/%2F"), RequestedHeartbeat = new TimeSpan(0, 0, 30), ContinuationTimeout = new TimeSpan(1, 0, 0) }; // Act serviceCollection.AddRabbitMqFactory(configuration); // Assert var services = serviceCollection.BuildServiceProvider(); services.GetRequiredService <IRabbitMqConfiguration>().Should().NotBeNull(); services.GetRequiredService <IRabbitMqConfiguration>().Should().BeEquivalentTo(configuration); var connectionFactory = services.GetRequiredService <IConnectionFactory>() as ConnectionFactory; connectionFactory.Should().NotBeNull(); connectionFactory.Uri.Should().Be(configuration.Uri); connectionFactory.RequestedHeartbeat.Should().Be(configuration.RequestedHeartbeat.Value); connectionFactory.ContinuationTimeout.Should().Be(configuration.ContinuationTimeout.Value); }
public void AddRabbitMqFactory_WithHotnameOnly_ShouldProvideConnectionFactory() { // Arrange var serviceCollection = new ServiceCollection(); var configuration = new DefaultRabbitMqConfiguration { Hostname = "localhost" }; // Act serviceCollection.AddRabbitMqFactory(configuration); // Assert var services = serviceCollection.BuildServiceProvider(); var connectionFactory = services.GetRequiredService <IConnectionFactory>() as ConnectionFactory; connectionFactory.Should().NotBeNull(); connectionFactory.HostName.Should().Be(configuration.Hostname); connectionFactory.Port.Should().Be(5672); }
static void Main(string[] args) { var hostname = args.Length > 0 ? args[0] : "localhost"; var port = args.Length > 1 ? int.Parse(args[1]) : 5672; var configuration = new DefaultRabbitMqConfiguration { Hostname = hostname, Port = port }; var services = new ServiceCollection() .AddLogging() .AddRabbitMqFactory(configuration); using var serviceProvider = services.BuildServiceProvider(); var channelFactory = serviceProvider.GetRequiredService <IChannelFactory>(); using var channel = channelFactory.Create(); 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; var message = Encoding.UTF8.GetString(body); Console.WriteLine(" [x] Received {0}", message); }; channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); }
static void Main(string[] args) { var hostname = args.Length > 0 ? args[0] : "localhost"; var port = args.Length > 1 ? int.Parse(args[1]) : 5672; var configuration = new DefaultRabbitMqConfiguration { Hostname = hostname, Port = port }; var services = new ServiceCollection() .AddLogging() .AddRabbitMqFactory(configuration); using var serviceProvider = services.BuildServiceProvider(); var channelFactory = serviceProvider.GetRequiredService <IChannelFactory>(); using var channel = channelFactory.Create(); channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null); var message = "Hello World!"; var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body); Console.WriteLine(" [x] Sent {0}", message); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); }