Exemplo n.º 1
0
        private Task Publish(object message, IBasicProperties properties, string exchange, string routingKey)
        {
            var context = new PublishContext
            {
                DependencyResolver = config.DependencyResolver,
                Exchange           = exchange,
                RoutingKey         = routingKey,
                Message            = message,
                Properties         = properties ?? new BasicProperties()
            };

            if (!context.Properties.IsTimestampPresent())
            {
                context.Properties.Timestamp = new AmqpTimestamp(new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds());
            }

            if (!context.Properties.IsDeliveryModePresent())
            {
                context.Properties.DeliveryMode = 2; // Persistent
            }
            // ReSharper disable ImplicitlyCapturedClosure - MiddlewareHelper will not keep a reference to the lambdas
            return(MiddlewareHelper.GoAsync(
                       config.PublishMiddleware,
                       async(handler, next) => await handler.Handle(context, next),
                       () => taskQueue.Value.Add(async() =>
            {
                var body = messageSerializer.Serialize(context.Message, context.Properties);
                (await GetChannel(PublishMaxConnectAttempts)).BasicPublish(context.Exchange, context.RoutingKey, false,
                                                                           context.Properties, body);
            }).Unwrap()));
            // ReSharper restore ImplicitlyCapturedClosure
        }
Exemplo n.º 2
0
        private async Task Publish(object message, IMessageProperties properties, string exchange, string routingKey, bool mandatory)
        {
            var writableProperties = new MessageProperties(properties);

            if (!writableProperties.Timestamp.HasValue)
            {
                writableProperties.Timestamp = DateTime.UtcNow;
            }

            writableProperties.Persistent = true;


            var context = new PublishContext
            {
                Config     = config,
                Exchange   = exchange,
                RoutingKey = routingKey,
                Message    = message,
                Properties = writableProperties
            };


            await MiddlewareHelper.GoAsync(
                config.Middleware.Publish,
                async (handler, next) => await handler.Handle(context, next),
                async() =>
            {
                var body = messageSerializer.Serialize(message, writableProperties);
                await clientFactory().Publish(body, writableProperties, exchange, routingKey, mandatory);
            });
        }
Exemplo n.º 3
0
        private async Task <ConsumeResult> InvokeUsingBinding(object message, MessageContextData messageContextData, IBinding binding)
        {
            using (var context = new MessageContext
            {
                Config = config,
                Queue = queueName,
                Exchange = messageContextData.Exchange,
                RoutingKey = messageContextData.RoutingKey,
                Message = message,
                Properties = messageContextData.Properties,
                Binding = binding
            })
            {
                try
                {
                    await MiddlewareHelper.GoAsync(config.Middleware.Message,
                                                   async (handler, next) => await handler.Handle(context, next),
                                                   async() => { await binding.Invoke(context); });

                    await binding.Cleanup(context, ConsumeResult.Success);

                    return(ConsumeResult.Success);
                }
                catch (Exception invokeException)
                {
                    var exceptionContext = new ExceptionStrategyContext(context, invokeException);
                    HandleException(exceptionContext);

                    await binding.Cleanup(context, exceptionContext.ConsumeResult);

                    return(exceptionContext.ConsumeResult);
                }
            }
        }
Exemplo n.º 4
0
 /// <inheritdoc />
 public async Task Cleanup(IMessageContext context, ConsumeResult consumeResult)
 {
     using (var controllerContext = new ControllerMessageContext(context)
     {
         Controller = null
     })
     {
         await MiddlewareHelper.GoAsync(
             bindingInfo.CleanupMiddleware,
             async (handler, next) => await handler.Cleanup(controllerContext, consumeResult, next),
             () => Task.CompletedTask);
     }
 }
Exemplo n.º 5
0
        private async Task <bool> FilterAllowed(IControllerMessageContext context)
        {
            var allowed = false;
            await MiddlewareHelper.GoAsync(
                bindingInfo.FilterMiddleware,
                async (handler, next) => await handler.Filter(context, next),
                () =>
            {
                allowed = true;
                return(Task.CompletedTask);
            });

            return(allowed);
        }
Exemplo n.º 6
0
        /// <inheritdoc />
        public async Task Invoke(IMessageContext context)
        {
            var controller = dependencyResolver.Resolve(bindingInfo.ControllerType);

            using (var controllerContext = new ControllerMessageContext(context)
            {
                Controller = controller
            })
            {
                if (!await FilterAllowed(controllerContext))
                {
                    return;
                }


                await MiddlewareHelper.GoAsync(
                    bindingInfo.MessageMiddleware,
                    async (handler, next) => await handler.Handle(controllerContext, next),
                    async() => await messageHandler(controllerContext));
            }
        }