Ejemplo n.º 1
0
 public Task StopAsync(CancellationToken cancellationToken)
 {
     _messageHandler.Stop();
     return(Task.CompletedTask);
 }
Ejemplo n.º 2
0
        private static void Main(string[] args)
        {
            // get configuration
            var configSection       = Config.GetSection("RabbitMQ");
            var host                = configSection["Host"];
            var userName            = configSection["UserName"];
            var password            = configSection["Password"];
            var sqlConnectionString = Config.GetConnectionString("ShippingCN");

            var services = new ServiceCollection();

            services.AddDbContext <ShippingDbContext>(options => options.UseSqlServer(sqlConnectionString));

            //...add any other services needed
            //services.AddTransient<IProductRepository, ProductRepository>();
            //services.AddTransient<ICustomerRepository, CustomerRepository>();
            //services.AddTransient<IOrderRepository, EFOrderRepository>();
            //services.AddTransient<ILogisticsRepository, LogisticsRepository>();
            //services.AddTransient<IPackageRepository, EFPackageRepository>();

            services.AddTransient <ILogisticsService, LogisticsService>();

            services.AddTransient <IMessageHandlerCallback, EventHandler>();

            services.AddTransient <IMessagePublisher>((sp) =>
                                                      new RabbitMQMessagePublisher(host, userName, password, "Ball.com"));
            services.AddSingleton <IMessageHandler>((sp) =>
                                                    new RabbitMQMessageHandler(host, userName, password, "Ball.com", "Shipping", ""));

            // then build provider
            var serviceProvider = services.BuildServiceProvider();

            //var messageHandlerCallback = serviceProvider.GetService<IMessageHandlerCallback>();
            RabbitMQMessageHandler messageHandler = new RabbitMQMessageHandler(host, userName, password, "Ball.com", "OrderAPI", "");

            var dbContext = serviceProvider.GetService <ShippingDbContext>();

            var messagePublisher = serviceProvider.GetService <IMessagePublisher>();
            var logisticsService = serviceProvider.GetService <ILogisticsService>();

            SetupAutoMapper();

            //Policy
            //    .Handle<Exception>()
            //    .WaitAndRetry(5, r => TimeSpan.FromSeconds(5),
            //        (ex, ts) => { Console.WriteLine("Error connecting to DB. Retrying in 5 sec."); })
            //    .Execute(() => dbContext.Database.Migrate());

            // start event-handler
            var eventHandler = new EventHandler(messagePublisher, messageHandler, logisticsService, Config.GetConnectionString("ShippingCN"));

            eventHandler.Start();

            if (Env == "Development")
            {
                Console.WriteLine("Shipping service started. Press any key to stop...");
                Console.ReadKey(true);
                messageHandler.Stop();
            }
            else
            {
                Console.WriteLine("Shipping service started.");
                while (true)
                {
                    Thread.Sleep(10000);
                }
            }
        }