Beispiel #1
0
        protected virtual void OnConsumerRecieved(object sender, BasicDeliverEventArgs e)
        {
            var message = new QueueMessage
            {
                Subject = e.RoutingKey,
                Body    = Encoding.UTF8.GetString(e.Body)
            };

            var type = _commands[message.Subject];

            var mapperType = _registry.GetMapper(type);
            var mapper     = _factory.Create(mapperType);

            var function = new Func <QueueMessage, IRequest>(
                (t) => ((dynamic)mapper).ToRequest((dynamic)message));

            var command = (ICommand)function.Invoke(message);

            using (IServiceScope scope = _serviceProvider.CreateScope())
            {
                var services = scope.ServiceProvider;
                ICommandProcessor commandProcessor = services.GetRequiredService <ICommandProcessor>();
                commandProcessor.ExecuteAsync(command).GetAwaiter().GetResult();
            }

            var channel = _commandChannels[e.RoutingKey];

            channel.BasicAck(e.DeliveryTag, false);
        }
Beispiel #2
0
        public async Task EnqueueAsync(IRequest request,
                                       CancellationToken cancellationToken = default(CancellationToken))
        {
            if (request == null)
            {
                throw new InvalidOperationException($"Request cannot be null");
            }

            var mapperType = _mapperRegistry.GetMapper(request.GetType());
            var mapper     = _mapperFactory.Create(mapperType);

            var function = new Func <IRequest, QueueMessage>(
                (t) => ((dynamic)mapper).ToMessage((dynamic)request));

            var message = function.Invoke(request);

            var client = _clientFactory.Create();
            await client.SendAsync(message, cancellationToken);
        }