static async Task Main(string[] args)
        {
            var busContext = new RabbitMQBusContextBuilder()
                             .WithExchange("Chess.Exchange")
                             .CreateContext();

            var publisher = new EventPublisher <IConnection>(busContext);

            var topicfilters = new List <string> {
                "#"
            };

            using var receiver = busContext.CreateMessageReceiver("ChessApi.ConsoleClient", topicfilters);
            receiver.StartReceivingMessages();
            receiver.StartHandlingMessages(MessageReceived);

            Console.WriteLine($"Listening to all messages on exchange '{busContext.ExchangeName}'...");

            while (true)
            {
                Console.WriteLine("Send a GameFormed-event by entering the GameId of a new game.");
                Console.Write("GameId:");
                if (int.TryParse(Console.ReadLine(), out int gameId))
                {
                    var gameFormed = new GameFormed {
                        GameId = gameId
                    };
                    await publisher.PublishEventAsync(gameFormed);
                }
            }
        }
        public async Task EventIsSendToMessageSender()
        {
            var senderMock = new Mock <IMessageSender>(MockBehavior.Strict);

            senderMock.Setup(r => r.SendMessageAsync(It.IsAny <EventMessage>()))
            .Returns(EmptyTask);
            senderMock.Setup(r => r.Dispose());
            var contextMock = new Mock <IBusContext <string> >(MockBehavior.Strict);

            contextMock.Setup(bc => bc.CreateMessageSender())
            .Returns(senderMock.Object);
            var target = new EventPublisher <string>(contextMock.Object);

            var evt = new SomeEvent {
                SomeNumber = 5
            };
            await target.PublishEventAsync(evt);

            senderMock.Verify(r => r.SendMessageAsync(It.Is <EventMessage>(em =>
                                                                           em.Topic == "DDD.Core.Application.Test.SomeEvent" &&
                                                                           em.EventType == "SomeEvent" &&
                                                                           //Encoding.Unicode.GetString(em.Body) == "{\r\n  \"SomeNumber\": 5\r\n}"
                                                                           Encoding.Unicode.GetString(em.Body).Contains("\"SomeNumber\"") &&
                                                                           Encoding.Unicode.GetString(em.Body).Contains("5")
                                                                           )));
        }
        public void EventPublisher_PublishEventAsync_ThrowsNotFoundExceptionIfEventIsNotFound()
        {
            // arrange
            ResetMocks();
            _repo.Setup(x => x.GetApplicationEvent(It.IsAny <long>(), It.IsAny <int>())).Returns((ApplicationEvent)null);
            var target = new EventPublisher(_repo.Object, _producer.Object);

            //act & assert
            Assert.ThrowsAsync <NotFoundException>(async() => await target.PublishEventAsync <object>(Guid.NewGuid(), new Identity(), 1, new object()));
        }
        public async Task InternalDomainEventIsNotSendToMessageSender()
        {
            var senderMock = new Mock <IMessageSender>(MockBehavior.Strict);

            senderMock.Setup(r => r.SendMessageAsync(It.IsAny <EventMessage>()))
            .Returns(EmptyTask);
            senderMock.Setup(r => r.Dispose());
            var contextMock = new Mock <IBusContext <string> >(MockBehavior.Strict);

            contextMock.Setup(bc => bc.CreateMessageSender())
            .Returns(senderMock.Object);
            var target = new EventPublisher <string>(contextMock.Object);

            var evt = new SomeInternalEvent {
                SomeNumber = 42
            };
            await target.PublishEventAsync(evt);

            senderMock.Verify(r => r.SendMessageAsync(It.IsAny <EventMessage>()), Times.Never);
        }
        public async void EventPublisher_PublishEventAsync_CallsProducerWithTopicAndData()
        {
            // arrange
            ResetMocks();
            int eventId = 1;
            ApplicationEvent applicationEvent = new ApplicationEvent(1, "Application", eventId, "Topic");
            var data = new object();

            _repo.Setup(x => x.GetApplicationEvent(It.IsAny <long>(), It.IsAny <int>())).Returns(applicationEvent);
            var target = new EventPublisher(_repo.Object, _producer.Object);

            //act
            var result = await target.PublishEventAsync <object>(Guid.NewGuid(), new Identity(), eventId, data);

            // assert
            result.Match(
                err => { },
                x =>
            {
                _producer.Verify(s => s.SendEvent(applicationEvent, JsonConvert.SerializeObject(data)), Times.Once);
            }
                );
        }