public static IServiceCollection AddPublisher( this IServiceCollection services, RabbitMqConfig config) { return(services.AddSingleton(config) .AddSingleton <RabbitMqPublisher>()); }
public RabbitMqPublisher(RabbitMqConfig config) { _config = config; var factory = new ConnectionFactory { Uri = config.ConnectionString }; _connection = factory.CreateConnection(); _model = _connection.CreateModel(); }
public RabbitMqConsumer( RabbitMqConfig config, Func <BasicDeliverEventArgs, Task> onMessage) { var factory = new ConnectionFactory { Uri = config.ConnectionString, DispatchConsumersAsync = true }; _connection = factory.CreateConnection(); _model = _connection.CreateModel(); var consumer = new AsyncEventingBasicConsumer(_model); consumer.Received += async(_, message) => { await onMessage(message); _model.BasicAck(message.DeliveryTag, false); }; _model.BasicConsume(config.Destination, false, consumer); }