public async Task Handle(SegmentWasFlown message, IMessageHandlerContext context) { Console.WriteLine("New flight plan was received for {0}, miles = {1}", message.CustomerId, message.MilesFlown); Data.CustomerId = message.CustomerId; Data.TotalMilesFlown += message.MilesFlown; if (CanCustomerBePromotedToGold(message.CustomerId)) { Console.WriteLine("Customer {0} is now promoted to Gold", message.CustomerId, message.MilesFlown); await context.Publish(new CustomerWasPromotedToGold() { CustomerId = message.CustomerId }).ConfigureAwait(false); await RequestTimeout(context, TimeSpan.FromSeconds(15), new CalendarYearHasStarted()).ConfigureAwait(false); } }
static async Task Main() { LogManager.Use <DefaultFactory>() .Level(LogLevel.Error); var endpointConfiguration = new EndpointConfiguration("PreferredCustomerPolicy"); endpointConfiguration.UseSerialization <NewtonsoftSerializer>(); endpointConfiguration.Recoverability().Delayed(c => c.NumberOfRetries(0)); var transport = endpointConfiguration.UseTransport <MsmqTransport>(); transport.DisableDeadLetterQueueing(); var routing = transport.Routing(); routing.RegisterPublisher(typeof(CustomerHasPaid), "PreferredCustomerPolicy"); routing.RegisterPublisher(typeof(SegmentWasFlown), "PreferredCustomerPolicy"); var persistence = endpointConfiguration.UsePersistence <InMemoryPersistence>(); endpointConfiguration.SendFailedMessagesTo("error"); endpointConfiguration.AuditProcessedMessagesTo("audit"); endpointConfiguration.EnableInstallers(); endpointConfiguration.AuditSagaStateChanges("Particular.ServiceControl"); var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); Console.WriteLine("Press f to publish SegmentWasFlown event."); Console.WriteLine("Press p to publish CustomerHasPaid event."); Console.WriteLine("Press ENTER to exit"); var guid = Guid.NewGuid(); while (true) { var key = Console.ReadKey(); Console.WriteLine(); if (key.Key == ConsoleKey.Enter) { break; } if (key.Key == ConsoleKey.F) { Console.WriteLine($"Publishing SegmentWasFlown event for CustomerId id: {guid:N}"); var message = new SegmentWasFlown() { CustomerId = guid, MilesFlown = 10000 }; await endpointInstance.Publish(message) .ConfigureAwait(false); } if (key.Key == ConsoleKey.P) { Console.WriteLine($"Publishing CustomerHasPaid event for CustomerId id: {guid:N}"); var billedEvent = new CustomerHasPaid() { CustomerId = guid, DollarsPaid = 2500 }; await endpointInstance.Publish(billedEvent) .ConfigureAwait(false); } } ; await endpointInstance.Stop() .ConfigureAwait(false); }