public void PublishTest() { var exchangeName = TestsOptions.TestExchangeName; var queueName = TestsOptions.TestQueueName; using (var link = TestsOptions.GetLinkBuilder().Build()) { try { using (var producer = link.Producer .Exchange(async cfg => { var ex = await cfg.ExchangeDeclare(exchangeName, LinkExchangeType.Fanout, autoDelete: true); var q = await cfg.QueueDeclare(queueName, false, false, true, expires: TimeSpan.FromMinutes(1)); await cfg.Bind(q, ex); return(ex); }) .ConfirmsMode(false) .Build() ) { producer.PublishAsync(new LinkPublishMessage <byte[]>(new byte[0])) .GetAwaiter() .GetResult(); } } finally { link.Topology .Handler(async cfg => { try { var ex = await cfg.ExchangeDeclarePassive(exchangeName); var q = await cfg.QueueDeclarePassive(queueName); await cfg.ExchangeDelete(ex); await cfg.QueueDelete(q); } catch { // No-op } }) .WaitAsync() .GetAwaiter() .GetResult(); } } }
private void TopologyExchangeConfig( LinkExchangeType exchangeType, bool durable, bool autoDelete, string alternateExchange, bool delayed ) { using (var rabbitConnection = TestsOptions.GetConnection()) { var rabbitModel = rabbitConnection.CreateModel(); var exchangeName = TestsOptions.TestExchangeName; using (var link = TestsOptions.GetLinkBuilder().Build()) { Assert.ThrowsAny <Exception>(() => { rabbitModel.ExchangeDeclarePassive(exchangeName); }); rabbitModel = rabbitConnection.CreateModel(); link.Topology .Handler(async cfg => { var e1 = await cfg.ExchangeDeclare(exchangeName, exchangeType, durable, autoDelete, alternateExchange, delayed); var e2 = await cfg.ExchangeDeclare(exchangeName + "-second", exchangeType, durable, autoDelete, alternateExchange, delayed); await cfg.ExchangeDeclarePassive(exchangeName); await cfg.ExchangeDeclareDefault(); await cfg.Bind(e2, e1); await cfg.Bind(e2, e1, "test"); }) .WaitAsync() .GetAwaiter() .GetResult(); rabbitModel.ExchangeDeclarePassive(exchangeName); rabbitModel.ExchangeDeclarePassive(exchangeName + "-second"); link.Topology .Handler(async cfg => { var e1 = await cfg.ExchangeDeclarePassive(exchangeName); var e2 = await cfg.ExchangeDeclarePassive(exchangeName + "-second"); await cfg.ExchangeDelete(e1); await cfg.ExchangeDelete(e2); }) .WaitAsync() .GetAwaiter() .GetResult(); Assert.ThrowsAny <Exception>(() => { rabbitModel.ExchangeDeclarePassive(exchangeName); }); } } }