public async Task PublishEventAsync(DomainEvent domainEvent) { // Do not publish internal events if (!(domainEvent is InternalDomainEvent)) { using (IMessageSender sender = _busContext.CreateMessageSender()) { var message = ToEventMessage(domainEvent); await sender.SendMessageAsync(message); } } }
public void SendAndReceiveTest() { var sentMessage = new EventMessage { CorrelationId = Guid.NewGuid(), EventType = "string", Timestamp = 100000, Topic = "MVM.Klantbeheer.KlantGeregistreerd", Body = Encoding.Unicode.GetBytes("Klant is geregistreerd"), }; var receivedFlag = new AutoResetEvent(false); EventMessage receivedMessage = null; var builder = new RabbitMQBusContextBuilder() .WithExchange("MVM.Eventbus") .WithAddress("localhost", 5672) .WithCredentials("guest", "guest"); using (IBusContext busContext = builder.CreateContext()) // Act - receiver using (IMessageReceiver receiver = busContext.CreateMessageReceiver( queueName: "MVM.PolisService", topicFilters: new string[] { "MVM.Klantbeheer.KlantGeregistreerd" })) { receiver.StartReceivingMessages(); receiver.StartHandlingMessages((EventMessage message) => { receivedMessage = message; receivedFlag.Set(); }); // Act - sender var sender = busContext.CreateMessageSender(); sender.SendMessageAsync(sentMessage).Wait(); // Assert bool messageHasBeenReveived = receivedFlag.WaitOne(2000); Assert.IsTrue(messageHasBeenReveived); Assert.AreEqual(sentMessage.CorrelationId, receivedMessage.CorrelationId); Assert.AreEqual(sentMessage.EventType, receivedMessage.EventType); Assert.AreEqual(sentMessage.Timestamp, receivedMessage.Timestamp); Assert.AreEqual(sentMessage.Topic, receivedMessage.Topic); CollectionAssert.AreEqual(sentMessage.Body, receivedMessage.Body); } }
public EventPublisher(IBusContext <IConnection> context) { Sender = context.CreateMessageSender(); }
/// <summary> /// Create a publisher and initialize a sender /// </summary> public EventPublisher(IBusContext <IConnection> context, ILoggerFactory loggerFactory = null) { Sender = context.CreateMessageSender(); loggerFactory ??= new NullLoggerFactory(); Logger = loggerFactory.CreateLogger <EventPublisher>(); }