Beispiel #1
0
        /// <inheritdoc/>
        public async Task <Option <MongoRecord> > Process(MongoRecord record, CancellationToken token)
        {
            logger.LogInformation("Message({MessageName}/{MessageId}) handling: started.", record.MessageName, record.Id);
            try
            {
                var client   = clientFactory.Create(MongoOptionsNames.DefaultName);
                var response = await client.RequestObject(record.Message, token);

                return(record.Succeed(response, clock.UtcNow).AsOption());
            }
            catch (Exception ex)
            {
                var clientOptions = options.Get(MongoOptionsNames.DefaultName);
                if (ex is MessageDeferredException or TimeoutException or OperationCanceledException ||
                    clientOptions.TransientExceptions.Any(x => x.IsInstanceOfType(ex)))
                {
                    logger.LogInformation(ex, "Message({MessageType}/{MessageId}) handling: deferred or transient error.", record.MessageName, record.Id);
                    return(Option.None);
                }

                logger.LogError(ex, "Message({MessageType}/{MessageId}) handling: permanent error.", record.MessageName, record.Id);
                return(record.Fail(converter.ConvertTo(ex) !, clock.UtcNow).AsOption());
            }
        }
Beispiel #2
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            if (context.Request.Method != HttpMethods.Post ||
                !context.Request.Path.StartsWithSegments("/messages"))
            {
                await next(context);

                return;
            }

            var serverOptions = options.CurrentValue;

            var message = await context.ReadMessageObject();

            if (!serverOptions.MessageTypes.Contains(message.GetType()))
            {
                throw new MessageNotRegisteredException(message.GetType());
            }

            var client   = clientFactory.Create(WebOptionsNames.DefaultName);
            var response = await client.RequestObject(message);

            await context.WriteMessageResponse(StatusCodes.Status200OK, response);
        }