示例#1
0
 public static IServiceCollection AddPublisher(
     this IServiceCollection services,
     RabbitMqConfig config)
 {
     return(services.AddSingleton(config)
            .AddSingleton <RabbitMqPublisher>());
 }
示例#2
0
        public RabbitMqPublisher(RabbitMqConfig config)
        {
            _config = config;

            var factory = new ConnectionFactory
            {
                Uri = config.ConnectionString
            };

            _connection = factory.CreateConnection();
            _model      = _connection.CreateModel();
        }
示例#3
0
        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);
        }