/// <summary> /// Enable the Dafda outbox producer implementation, configurable using the <paramref name="options"/> /// </summary> /// <param name="services">The service collection</param> /// <param name="options">Configure the <see cref="OutboxProducerOptions"/></param> public static void AddOutboxProducer(this IServiceCollection services, Action <OutboxProducerOptions> options) { var builder = new ProducerConfigurationBuilder(); var outboxProducerOptions = new OutboxProducerOptions(builder, services); options?.Invoke(outboxProducerOptions); var configuration = builder.Build(); var outboxListener = outboxProducerOptions.OutboxListener; if (outboxListener == null) { throw new InvalidConfigurationException($"No {nameof(IOutboxListener)} was registered. Please use the {nameof(OutboxProducerOptions.WithListener)} in the {nameof(AddOutboxProducer)} configuration."); } services.AddTransient <IHostedService, OutboxDispatcherHostedService>(provider => { var outboxUnitOfWorkFactory = provider.GetRequiredService <IOutboxUnitOfWorkFactory>(); var loggerFactory = provider.GetRequiredService <ILoggerFactory>(); var kafkaProducer = configuration.KafkaProducerFactory(loggerFactory); var producer = new OutboxProducer(kafkaProducer); var outboxDispatcher = new OutboxDispatcher(loggerFactory, outboxUnitOfWorkFactory, producer); return(new OutboxDispatcherHostedService(outboxListener, outboxDispatcher)); }); }
public static void AddOutboxProducer(this IServiceCollection services, Action <OutboxProducerOptions> options) { var builder = new ProducerConfigurationBuilder(); var outboxProducerOptions = new OutboxProducerOptions(builder, services); options?.Invoke(outboxProducerOptions); var configuration = builder.Build(); services.AddTransient <IHostedService, OutboxDispatcherHostedService>(provider => { var outboxUnitOfWorkFactory = provider.GetRequiredService <IOutboxUnitOfWorkFactory>(); var kafkaProducer = configuration.KafkaProducerFactory(); var producer = new OutboxProducer(kafkaProducer); var outboxNotification = provider.GetRequiredService <IOutboxNotification>(); return(new OutboxDispatcherHostedService( outboxUnitOfWorkFactory, producer, outboxNotification )); }); }