static async Task RunApplication(BookShopConsoleOptions options) { System.Console.WriteLine("[START] BookShop.Console - RunApplication"); var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); var serviceProvider = new ServiceCollection() .AddProducerInfraMessaging(Configuration) .AddInfraNet(Configuration) .BuildServiceProvider(); var messageProducer = serviceProvider.GetService <IMessageProducer>(); var messageConfiguration = new MessageProducerConfiguration { Topic = "BookShop.NewRequest" }; var numbers = new int[options?.NumberMessages ?? 1]; System.Console.WriteLine($"Number messages: {numbers.Length}"); var books = await GetBooks(serviceProvider); var countBooks = books.Count(); var randomBooks = new Random(); var randomQuantity = new Random(); var tasks = numbers.Select(async number => { var bookIndex = randomBooks.Next(0, countBooks - 1); var book = books[bookIndex]; var value = new Request { BookId = book.BookId, Quantity = randomQuantity.Next(1, 1000) }; try { var deliveryResult = await messageProducer.ProduceAsync(messageConfiguration, value); System.Console.WriteLine($"=> Offset: {deliveryResult?.Offset.Value}"); } catch (Exception ex) { System.Console.WriteLine(ex.Message); } }); await Task.WhenAll(tasks); System.Console.WriteLine("[FINISH] BookShop.Console - RunApplication"); }
static async Task Main(string[] args) { System.Console.WriteLine("[START] BookShop.Console - Main"); var options = new BookShopConsoleOptions(); await RunApplication(options); System.Console.WriteLine("[FINISH] BookShop.Console - Main"); }