protected override async Task ExecuteAsync(CancellationToken stoppingToken) { using (var scope = _serviceScopeFactory.CreateScope()) { var _paymentRepository = scope.ServiceProvider.GetRequiredService <IPaymentRepository>(); await ConsumerWrapper.Consume(_consumerConfig, stoppingToken, "handle-payment-error", async (cr) => { var entity = JsonConvert.DeserializeObject <PaymentSagaEntity>(cr.Message.Value); await _paymentRepository.DeletePayment(entity.paymentId); }); } }
static void Main(string[] args) { ConsumerWrapper consumerWrapper = new ConsumerWrapper(); consumerWrapper.Consume(); // var config = new ConsumerConfig // { // GroupId = "Loyalty_Consumer", // BootstrapServers = "kafka:9092", // AutoOffsetReset = AutoOffsetReset.Earliest // }; // using (var consumer = new ConsumerBuilder<Ignore, string>(config).Build()) // { // consumer.Subscribe("testtopic"); // CancellationTokenSource cts = new CancellationTokenSource(); // Console.CancelKeyPress += (_, e) => // { // e.Cancel = true; // cts.Cancel(); // }; // try // { // while (true) // { // try // { // var cr = consumer.Consume(cts.Token); // Console.WriteLine("LoyaltyService.Consumer:" + cr.Message.Value); // } // catch (ConsumeException e) // { // Console.WriteLine($"Error occured: {e.Error.Reason}"); // } // } // } // catch (OperationCanceledException) // { // consumer.Close(); // } // } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { using (var scope = _serviceScopeFactory.CreateScope()) { var _paymentRepository = scope.ServiceProvider.GetRequiredService <IPaymentRepository>(); await ConsumerWrapper.Consume(_consumerConfig, stoppingToken, "handle-payment", async (cr) => { var entity = JsonConvert.DeserializeObject <PaymentEntity>(cr.Message.Value); var payResult = await _paymentRepository.Pay(entity.Id); if (!payResult.Success) { var data = new PaymentSagaEntity(entity.Id, payResult.Message); ProducerWrapper.ProduceAsync(_producerConfig, "handle-payment-error", data); } }); } }