|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Microsoft.Data.SqlClient; |
| 4 | +using NServiceBus; |
| 5 | + |
| 6 | +class Program |
| 7 | +{ |
| 8 | + static async Task Main() |
| 9 | + { |
| 10 | + Console.Title = "RabbitMQOutbox"; |
| 11 | + |
| 12 | + #region ConfigureTransport |
| 13 | + var endpointConfiguration = new EndpointConfiguration("Samples.RabbitMQ.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 | + #region ConfigurePersistence |
| 24 | + // for SqlExpress use Data Source=.\SqlExpress;Initial Catalog=NsbRabbitMqOutbox;Integrated Security=True;Max Pool Size=100;Encrypt=false |
| 25 | + // Password must match value in docker-compose.yml |
| 26 | + var connectionString = @"Server=localhost,11433;Initial Catalog=NsbRabbitMqOutbox;User Id=SA;Password=NServiceBus!;Max Pool Size=100;Encrypt=false"; |
| 27 | + |
| 28 | + var persistence = endpointConfiguration.UsePersistence<SqlPersistence>(); |
| 29 | + persistence.SqlDialect<SqlDialect.MsSqlServer>(); |
| 30 | + persistence.ConnectionBuilder(() => new SqlConnection(connectionString)); |
| 31 | + #endregion |
| 32 | + |
| 33 | + endpointConfiguration.EnableInstallers(); |
| 34 | + |
| 35 | + #region SampleSteps |
| 36 | + |
| 37 | + // STEP 1: Run code as is, duplicates can be observed in console and database |
| 38 | + |
| 39 | + // STEP 2: Uncomment this line to enable the Outbox. Duplicates will be suppressed. |
| 40 | + // endpointConfiguration.EnableOutbox(); |
| 41 | + |
| 42 | + // STEP 3: Comment out this line to allow concurrent processing. Concurrency exceptions will |
| 43 | + // occur in the console window, but only 5 entries will be made in the database. |
| 44 | + endpointConfiguration.LimitMessageProcessingConcurrencyTo(1); |
| 45 | + |
| 46 | + #endregion |
| 47 | + |
| 48 | + Helper.EnsureDatabaseExists(connectionString); |
| 49 | + |
| 50 | + var endpointInstance = await Endpoint.Start(endpointConfiguration); |
| 51 | + |
| 52 | + Console.WriteLine("Endpoint started. Press Enter to send 5 sets of duplicate messages..."); |
| 53 | + Console.ReadLine(); |
| 54 | + |
| 55 | + for (var i = 0; i < 5; i++) |
| 56 | + { |
| 57 | + var myMessage = new MyMessage(); |
| 58 | + await Helper.SendDuplicates(endpointInstance, myMessage, totalCount: 2); |
| 59 | + } |
| 60 | + |
| 61 | + await Task.Delay(5000); |
| 62 | + Console.WriteLine("Press any key to exit"); |
| 63 | + Console.ReadKey(); |
| 64 | + await endpointInstance.Stop(); |
| 65 | + } |
| 66 | +} |
0 commit comments