Esempio n. 1
0
        protected async Task <CommandExecutionResult> ProcessMessage(string message, CancellationToken token)
        {
            try
            {
                var messageJObject = JObject.Parse(message);
                var identity       = messageJObject.GetValue("identity", StringComparison.OrdinalIgnoreCase)?.Value <string>();

                if (string.IsNullOrEmpty(identity))
                {
                    return(CommandExecutionResult.Failure($"Failure while processing command, identity is missing, message: {message}"));
                }

                var result = await ProcessCommand(identity, message, token);

                switch (result.Status)
                {
                case CommandExecutionStatus.Undefined:
                    throw new MicroserviceException(
                              $"Cannot process command, identity '{identity}', body: {message}");

                case CommandExecutionStatus.NotRecognized:
                    throw new MicroserviceException(
                              "NotRecognized is never processed on this level, check the code!!!");

                case CommandExecutionStatus.Success:
                case CommandExecutionStatus.Failure:
                case CommandExecutionStatus.CriticalFailure:
                    return(result);

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (OperationCanceledException e)
            {
                Console.WriteLine($"Service {Options.ServiceId}, cancellation requested. Cancellation exception: {e}");
                throw;
            }
            catch (MicroserviceException e)
            {
                var errorMessage = $"ATTENTION! CRITICAL EXCEPTION occured in service {Options.ServiceId} while processing message, exception: {e}, message text: {message}";
                Console.WriteLine(errorMessage);
                return(CommandExecutionResult.CriticalFailure(errorMessage, e));
            }
            catch (Exception e)
            {
                var errorMessage = $"Exception occured in service {Options.ServiceId} while processing message, exception: {e}, message text: {message}";
                Console.WriteLine(errorMessage);
                throw;
            }
        }
Esempio n. 2
0
        public override async Task <CommandExecutionResult> Execute(PostProcessOrderCommand command)
        {
            if (command.OrderId <= 0)
            {
                return(CommandExecutionResult.ValidationFailure($"Invalid order ID: {command.OrderId}"));
            }

            var orderId = command.OrderId;

            Console.WriteLine($"Post-process order id = {orderId}");

            Console.WriteLine("Getting order details...");
            var orderDetailsResult = await _web1.GetOrderDetails(orderId);

            if (orderDetailsResult.Status != SendStatus.Success || orderDetailsResult.Result == null)
            {
                Console.WriteLine("Getting order details failed.");
                return(CommandExecutionResult.Failure(orderDetailsResult.ErrorMessage, orderDetailsResult.ErrorReason));
            }

            var orderDetails = orderDetailsResult.Result;

            Console.WriteLine($"Retrieved order details: user is (id {orderDetails.UserId}, name {orderDetails.UserName}), order amount is {orderDetails.Amount}, created on {orderDetails.DateCreated}");

            Console.WriteLine($"Pass order details to Azure Functions HTTP microservice...");

            var captureResult = await _azureFunctionsHttpServiceClient.CaptureOrder(orderDetails.OrderId, orderDetails.UserName);

            if (captureResult.Status != SendStatus.Success)
            {
                Console.WriteLine($"Calling Azure Functions microservice failed: {captureResult.ErrorMessage}");
            }
            else
            {
                Console.WriteLine($"Azure Functions microservice replied: '{captureResult.Result.Message}'");
            }

            return(CommandExecutionResult.Ok());
        }