|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using NServiceBus; |
| 4 | + |
| 5 | +static class Program |
| 6 | +{ |
| 7 | + static async Task Main() |
| 8 | + { |
| 9 | + Console.Title = "LeftSender"; |
| 10 | + var endpointConfiguration = new EndpointConfiguration("Samples.Bridge.LeftSender"); |
| 11 | + endpointConfiguration.UsePersistence<LearningPersistence>(); |
| 12 | + |
| 13 | + endpointConfiguration.Conventions().DefiningCommandsAs(t => t.Name == "PlaceOrder"); |
| 14 | + endpointConfiguration.Conventions().DefiningMessagesAs(t => t.Name == "OrderResponse"); |
| 15 | + endpointConfiguration.Conventions().DefiningEventsAs(t => t.Name == "OrderReceived"); |
| 16 | + |
| 17 | + endpointConfiguration.UseSerialization<SystemJsonSerializer>(); |
| 18 | + var routing = endpointConfiguration.UseTransport(new LearningTransport()); |
| 19 | + routing.RouteToEndpoint(typeof(PlaceOrder), "Samples.Bridge.RightReceiver"); |
| 20 | + |
| 21 | + endpointConfiguration.SendFailedMessagesTo("error"); |
| 22 | + endpointConfiguration.EnableInstallers(); |
| 23 | + |
| 24 | + var endpointInstance = await Endpoint.Start(endpointConfiguration); |
| 25 | + await Start(endpointInstance); |
| 26 | + await endpointInstance.Stop(); |
| 27 | + } |
| 28 | + |
| 29 | + static async Task Start(IEndpointInstance endpointInstance) |
| 30 | + { |
| 31 | + Console.WriteLine("Press '1' to send the PlaceOrder command"); |
| 32 | + Console.WriteLine("Press '2' to publish the OrderReceived event"); |
| 33 | + Console.WriteLine("Press 'esc' other key to exit"); |
| 34 | + |
| 35 | + while (true) |
| 36 | + { |
| 37 | + var key = Console.ReadKey(); |
| 38 | + Console.WriteLine(); |
| 39 | + |
| 40 | + var orderId = Guid.NewGuid(); |
| 41 | + switch (key.Key) |
| 42 | + { |
| 43 | + case ConsoleKey.D1: |
| 44 | + case ConsoleKey.NumPad1: |
| 45 | + var placeOrder = new PlaceOrder |
| 46 | + { |
| 47 | + OrderId = orderId |
| 48 | + }; |
| 49 | + await endpointInstance.Send(placeOrder); |
| 50 | + Console.WriteLine($"Send PlaceOrder Command with Id {orderId}"); |
| 51 | + break; |
| 52 | + case ConsoleKey.D2: |
| 53 | + case ConsoleKey.NumPad2: |
| 54 | + var orderReceived = new OrderReceived |
| 55 | + { |
| 56 | + OrderId = orderId |
| 57 | + }; |
| 58 | + await endpointInstance.Publish(orderReceived); |
| 59 | + Console.WriteLine($"Published OrderReceived Event with Id {orderId}."); |
| 60 | + break; |
| 61 | + case ConsoleKey.Escape: |
| 62 | + return; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments