|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Microsoft.Azure.Cosmos; |
| 4 | +using NServiceBus; |
| 5 | + |
| 6 | +class Program |
| 7 | +{ |
| 8 | + static async Task Main() |
| 9 | + { |
| 10 | + Console.Title = "RabbitMQCosmosDBOutbox"; |
| 11 | + |
| 12 | + #region ConfigureTransport |
| 13 | + var endpointConfiguration = new EndpointConfiguration("Samples.CosmosDb.Outbox"); |
| 14 | + |
| 15 | + var rabbitMqTransport = new RabbitMQTransport(RoutingTopology.Conventional(QueueType.Classic), "host=localhost;username=rabbitmq;password=rabbitmq") |
| 16 | + { |
| 17 | + TransportTransactionMode = TransportTransactionMode.ReceiveOnly |
| 18 | + }; |
| 19 | + endpointConfiguration.UseSerialization<SystemJsonSerializer>(); |
| 20 | + endpointConfiguration.UseTransport(rabbitMqTransport); |
| 21 | + #endregion |
| 22 | + |
| 23 | + //add your own account here |
| 24 | + var connectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; |
| 25 | + |
| 26 | + #region Create CosmosDb resources |
| 27 | + |
| 28 | + await Helper.CreateContainerAndDbIfNotExists(connectionString); |
| 29 | + #endregion |
| 30 | + |
| 31 | + #region ConfigurePersistence |
| 32 | + |
| 33 | + var persistence = endpointConfiguration.UsePersistence<CosmosPersistence>(); |
| 34 | + |
| 35 | + endpointConfiguration.UsePersistence<CosmosPersistence>() |
| 36 | + .CosmosClient(new CosmosClient(connectionString)) |
| 37 | + .DatabaseName("Samples.Database.Demo"); |
| 38 | + |
| 39 | + persistence.DefaultContainer("Outbox", "/messageId"); |
| 40 | + #endregion |
| 41 | + |
| 42 | + endpointConfiguration.EnableInstallers(); |
| 43 | + |
| 44 | + #region SampleSteps |
| 45 | + |
| 46 | + // STEP 1: Run code as is, duplicates can be observed in console and database |
| 47 | + var transactionInformation = persistence.TransactionInformation(); |
| 48 | + transactionInformation.ExtractPartitionKeyFromHeader("PartitionKeyHeader"); |
| 49 | + |
| 50 | + // STEP 2: Uncomment this line to enable the Outbox. Duplicates will be suppressed. |
| 51 | + // endpointConfiguration.EnableOutbox(); |
| 52 | + |
| 53 | + // STEP 3: Comment out this line to allow concurrent processing. Concurrency exceptions will |
| 54 | + // occur in the console window, but only 5 entries will be made in the database. |
| 55 | + endpointConfiguration.LimitMessageProcessingConcurrencyTo(1); |
| 56 | + |
| 57 | + #endregion |
| 58 | + var endpointInstance = await Endpoint.Start(endpointConfiguration); |
| 59 | + |
| 60 | + Console.WriteLine("Endpoint started. Press Enter to send 5 sets of duplicate messages..."); |
| 61 | + Console.ReadLine(); |
| 62 | + |
| 63 | + for (var i = 0; i < 5; i++) |
| 64 | + { |
| 65 | + var myMessage = new MyMessage(); |
| 66 | + await Helper.SendDuplicates(endpointInstance, myMessage, totalCount: 2); |
| 67 | + } |
| 68 | + |
| 69 | + await Task.Delay(5000); |
| 70 | + Console.WriteLine("Press any key to exit"); |
| 71 | + Console.ReadKey(); |
| 72 | + await endpointInstance.Stop(); |
| 73 | + } |
| 74 | +} |
0 commit comments